forked from KuiMing/dsp_ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdsp_ggplot2.html
More file actions
1619 lines (1473 loc) · 63.7 KB
/
Copy pathdsp_ggplot2.html
File metadata and controls
1619 lines (1473 loc) · 63.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>EDA with R</title>
<meta charset="utf-8">
<meta name="description" content="EDA with R">
<meta name="author" content="Ben Chen, Johnson Hsieh">
<meta name="generator" content="slidify" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="libraries/frameworks/io2012-dsp/css/default.css" media="all" >
<link rel="stylesheet" href="libraries/frameworks/io2012-dsp/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="libraries/frameworks/io2012-dsp/css/slidify.css" >
<link rel="stylesheet" href="libraries/highlighters/highlight.js/css/zenburn.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. --> <link rel=stylesheet href="./assets/css/dsp.css"></link>
<link rel=stylesheet href="./assets/css/ribbons.css"></link>
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="libraries/frameworks/io2012-dsp/js/jquery-1.7.min.js"></script>
<!-- <script>window.jQuery || document.write('<script src="libraries/widgets/quiz/js/jquery.js"><\/script>')</script> -->
<script data-main="libraries/frameworks/io2012-dsp/js/slides"
src="libraries/frameworks/io2012-dsp/js/require-1.0.8.min.js">
</script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- LOGO SLIDE -->
<slide class="title-slide segue nobackground">
<hgroup class="auto-fadein">
<h1>EDA with R</h1>
<h2>Data Visualization with ggplot</h2>
<p>Ben Chen, Johnson Hsieh<br/></p>
</hgroup>
<article></article>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>The Anatomy of a Plot</h2>
</hgroup>
<article data-timings="">
<p><center>
<img src='./img/anatomy.png' width=900 align='center'></img>
</center></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-2" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Data Visualization</h2>
</hgroup>
<article data-timings="">
<ul>
<li>清晰有效地傳達與溝通訊息</li>
<li>教學、研究、宣傳</li>
<li>美學、功能兼顧</li>
<li>統計圖形、訊息可視化</li>
<li>參考Johnson於DSHC meetp的<a href="http://goo.gl/xYorRm">DataViz 介紹</a></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-3" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>ggplot2簡介</h2>
</hgroup>
<article data-timings="">
<ul>
<li>2015年,最受歡迎的R套件之一</li>
<li>R環境下的繪圖套件</li>
<li>取自 “The Grammar of Graphics” (Leland Wilkinson, 2005)</li>
<li>設計理念
<ul>
<li>採用圖層系統</li>
<li>用抽象的概念來控制圖形,避免細節繁瑣</li>
<li>圖形美觀</li>
</ul></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-4" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>ggplot2基本架構</h2>
</hgroup>
<article data-timings="">
<ul>
<li>資料 (data) 和映射 (mapping)</li>
<li>幾何對象 (<code>geom</code>etric)</li>
<li>座標尺度 (<code>scale</code>)</li>
<li>統計轉換 (<code>stat</code>istics)</li>
<li>座標系統 (<code>coord</code>inante)</li>
<li>圖層 (layer)</li>
<li>刻面 (<code>facet</code>)</li>
<li>主題 (<code>theme</code>)</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-5" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>ggplot2 基本語法</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(data=..., aes(x=..., y=...)) + geom_xxx(...) +
stat_xxx(...) + facet_xxx(...) + ...
</code></pre>
<ul>
<li><code>ggplot</code> 描述 data 從哪來</li>
<li><code>aes</code> 描述圖上的元素跟 data 之類的對應關係</li>
<li><code>geom_xxx</code> 描述要畫圖的類型及相關調整的參數</li>
<li>常用的類型諸如:<code>geom_bar</code>, <code>geom_points</code>, <code>geom_line</code>, <code>geom_polygon</code></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-6" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>注意事項</h2>
</hgroup>
<article data-timings="">
<ul>
<li>使用 <code>data.frame</code> 儲存資料 (不可以丟 <code>matrix</code> 物件)</li>
<li>使用 long format (利用<code>reshape2</code>將資料轉換成 1 row = 1 observation)</li>
<li>文字型態的資料預設依 ascii 編碼順序做排序</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>需要的套件</h2>
</hgroup>
<article data-timings="">
<pre><code class="r"># install.packages(c("ggplot2", "data.table", "dplyr", "reshape2"), repos = "http://cran.csie.ntu.edu.tw/")
library(ggplot2)
library(data.table)
library(dplyr)
library(reshape2)
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-8" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>一切從讀檔開始 (CSV)</h2>
</hgroup>
<article data-timings="">
<p><a href="http://goo.gl/8itFhs">YouBike-Weather Data</a></p>
<pre><code class="r"># 讀檔起手式
ubike <- read.csv('ubikebyhourutf8/ubike-hour-201502-utf8.csv') #請輸入正確的檔案路徑
# 讀檔進階招式
ubike <- read.csv('檔案路徑',
colClasses = c("factor","integer","integer","factor","factor",
"numeric","numeric","integer","numeric","integer",
"integer","numeric","numeric", "integer","integer",
"numeric","numeric","numeric", "numeric","numeric",
"numeric"))
# 讀檔大絕招
ubike <- fread('檔案路徑',
data.table = FALSE,
colClasses = c("factor","integer","integer","factor",
"factor","numeric", "numeric", "integer",
"numeric", "integer","integer","numeric",
"numeric", "integer","integer","numeric",
"numeric","numeric", "numeric","numeric",
"numeric"))
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>展示資料</h2>
</hgroup>
<article data-timings="">
<pre><code> date hour sno sarea sna lat lng
1 2015-02-01 0 1 信義區 捷運市政府站(3號出口) 25.04086 121.5679
2 2015-02-01 0 2 大安區 捷運國父紀念館站(2號出口) 25.04100 121.5569
3 2015-02-01 0 3 信義區 台北市政府 25.03780 121.5652
4 2015-02-01 0 5 信義區 興雅國中 25.03656 121.5687
5 2015-02-01 0 7 信義區 信義廣場(台北101) 25.03304 121.5656
6 2015-02-01 0 8 信義區 世貿三館 25.03521 121.5637
tot avg.sbi max.sbi min.sbi std.sbi avg.bemp max.bemp min.bemp std.bemp
1 180 4.153 11 0 3.643 175.847 180 169 3.643
2 48 1.322 5 0 1.383 46.678 48 43 1.383
3 40 0.407 1 0 0.495 39.593 40 39 0.495
4 60 1.220 3 0 0.966 57.780 59 56 0.966
5 80 2.949 9 0 3.345 75.983 79 69 3.476
6 60 1.983 7 0 2.543 57.017 59 52 2.543
temp humidity pressure max.anemo rainfall
1 13.73755 87.49700 1025.791 1.7773507 0
2 13.80401 86.81732 1025.411 0.8515215 0
3 13.82918 87.25139 1025.754 1.6134319 0
4 13.83382 87.56932 1025.875 1.9129308 0
5 13.92348 87.48805 1025.863 1.9093223 0
6 13.88992 87.21782 1025.738 1.6235643 0
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-10" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>欄位說明</h2>
</hgroup>
<article data-timings="">
<pre>
name.eng name.cht name.eng name.cht
1 date 日期 12 std.sbi 車輛數標準差
2 hour 時間 13 avg.bemp 平均空位數
3 sno 場站代號 14 max.bemp 最大空位數
4 sarea 場站區域 15 min.bemp 最小空位數
5 sna 場站名稱 16 std.bemp 空位數標準差
6 lat 緯度 17 temp 平均氣溫
7 lng 經度 18 humidity 溼度
8 tot 總停車格 19 pressure 氣壓
9 avg.sbi 平均車輛數 20 max.anemo 最大風速
10 max.sbi 最大車輛數 21 rainfall 降雨量
11 min.sbi 最小車輛數
</pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-11" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>單一數值:Histogram</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Histogram</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>geom_histogram</code></li>
</ul>
<pre><code class="r">thm <- function() theme(text=element_text(size=20, family="STHeiti")) # 控制字體與大小
# STHeiti是只有Mac才有的字體, 用來解決Mac系統中文顯示錯誤的問題
# Windows系統的使用者請忽略 `+ thm()` 指令
ggplot(ubike) +
geom_histogram(aes(x=max.anemo, y=..count..)) + thm()
</code></pre>
<p><img src="assets/fig/wind1-1.png" title="plot of chunk wind1" alt="plot of chunk wind1" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Histogram</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>aes(y=..count..)</code> vs. <code>aes(y=..density..)</code></li>
</ul>
<pre><code class="r">ggplot(ubike) +
geom_histogram(aes(x=max.anemo, y=..density..)) + thm()
</code></pre>
<p><img src="assets/fig/wind2-1.png" title="plot of chunk wind2" alt="plot of chunk wind2" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Histogram</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>aes(x=..., y=..., fill=...)</code></li>
</ul>
<pre><code class="r">ggplot(ubike) +
geom_histogram(aes(x=max.anemo, y=..density.., fill=..count..)) + thm()
</code></pre>
<p><img src="assets/fig/wind3-1.png" title="plot of chunk wind3" alt="plot of chunk wind3" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Histogram + Density</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>geom_histogram() + geom_density()</code></li>
</ul>
<pre><code class="r">ggplot(ubike, aes(x=max.anemo)) +
geom_histogram(aes(y=..density.., fill=..count..)) +
geom_density() + thm()
</code></pre>
<p><img src="assets/fig/wind4-1.png" title="plot of chunk wind4" alt="plot of chunk wind4" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-16" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>量化 v.s. 量化:Scatter Plot</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-17" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>繪圖之前的整理資料</h2>
</hgroup>
<article data-timings="">
<p>文山區各站點在"2015-02"的平均溼度 vs. 平均雨量</p>
<pre><code class="r">x3 <- filter(ubike, grepl("2015-02", date, fixed = TRUE), sarea == "文山區") %>%
group_by(sna) %>%
summarise(rain.avg = mean(rainfall), hum.avg = mean(humidity))
x3 # print(x3)
</code></pre>
<pre><code>Source: local data frame [12 x 3]
sna rain.avg hum.avg
(fctr) (dbl) (dbl)
1 國立政治大學 0.9138340 86.78447
2 捷運動物園站(2號出口) 0.6944844 86.28648
3 捷運景美站 0.6752715 79.55976
4 捷運木柵站 0.6601309 85.49780
5 考試院 0.9153789 80.25694
6 羅斯福景隆街口 0.6036172 80.38156
7 師範大學公館校區 0.7153086 80.71318
8 台北花木批發市場 0.6517227 80.96029
9 臺北市立景美女中 1.0727852 81.86252
10 文山行政中心 0.8457266 85.15373
11 文山運動中心 0.6683047 82.91816
12 興豐公園 0.6461699 81.34372
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Scatter plot</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>geom_point()</code></li>
<li>參數<code>size</code>放在<code>aes()</code>函數外面,表示所有的資料固定用相同的size</li>
</ul>
<pre><code class="r">ggplot(x3) +
geom_point(aes(x = rain.avg, y = hum.avg), size=5) + #size控制點的大小
thm()
</code></pre>
<p><img src="assets/fig/ubike.site.wet.rainfall2-1.png" title="plot of chunk ubike.site.wet.rainfall2" alt="plot of chunk ubike.site.wet.rainfall2" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-19" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Grouped scatter plot</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(x3) +
# 放在aes裡的 colour 和 size 可依資料調整顏色和大小
geom_point(aes(x = rain.avg, y = hum.avg, colour=sna, size=rain.avg)) +
# 限制大小
scale_size(range=c(5,10)) +
thm()
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-20" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Grouped scatter plot</h2>
</hgroup>
<article data-timings="">
<p><img src="assets/fig/ubike.site.wet.rainfall3-1.png" title="plot of chunk ubike.site.wet.rainfall3" alt="plot of chunk ubike.site.wet.rainfall3" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-21" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>量化 v.s. 量化:Line Chart</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-22" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>WorldPhones</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">data(WorldPhones)
WorldPhones
</code></pre>
<pre><code> N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
1951 45939 21574 2876 1815 1646 89 555
1956 60423 29990 4708 2568 2366 1411 733
1957 64721 32510 5230 2695 2526 1546 773
1958 68484 35218 6662 2845 2691 1663 836
1959 71799 37598 6856 3000 2868 1769 911
1960 76036 40341 8220 3145 3054 1905 1008
1961 79831 43173 9053 3338 3224 2005 1076
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-23" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>小挑戰</h2>
</hgroup>
<article data-timings="">
<p>如何畫出每年亞洲電話數量的Line chart?</p>
<pre><code>ggplot(WorldPhones, aes(x=?????, y=Asia)) + ......
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-24" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>哪裏不對?</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(WorldPhones)
</code></pre>
<pre><code>Error: ggplot2 doesn't know how to deal with data of class matrix
</code></pre>
<pre><code class="r">class(WorldPhones)
</code></pre>
<pre><code>[1] "matrix"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-25" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2><code>matrix</code> 轉換 <code>data.frame</code></h2>
</hgroup>
<article data-timings="">
<pre><code class="r">WP.df <- as.data.frame(WorldPhones)
WP.df$year <- rownames(WP.df)
class(WP.df)
</code></pre>
<pre><code>[1] "data.frame"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-26" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Line chart</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>geom_line()</code></li>
</ul>
<pre><code class="r">ggplot(WP.df, aes(x=year, y=Asia)) + geom_line()
</code></pre>
<pre><code>geom_path: Each group consists of only one observation. Do you need to
adjust the group aesthetic?
</code></pre>
<p><img src="assets/fig/wp4 -1.png" title="plot of chunk wp4 " alt="plot of chunk wp4 " style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-27" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Should be Number</h2>
</hgroup>
<article data-timings="">
<ul>
<li><code>as.numeric()</code></li>
</ul>
<pre><code class="r">str(WP.df)
</code></pre>
<pre><code>'data.frame': 7 obs. of 8 variables:
$ N.Amer : num 45939 60423 64721 68484 71799 ...
$ Europe : num 21574 29990 32510 35218 37598 ...
$ Asia : num 2876 4708 5230 6662 6856 ...
$ S.Amer : num 1815 2568 2695 2845 3000 ...
$ Oceania : num 1646 2366 2526 2691 2868 ...
$ Africa : num 89 1411 1546 1663 1769 ...
$ Mid.Amer: num 555 733 773 836 911 ...
$ year : chr "1951" "1956" "1957" "1958" ...
</code></pre>
<pre><code class="r">WP.df$year <- as.numeric(WP.df$year)
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-28" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Line chart</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(WP.df, aes(x=year, y=Asia)) +
geom_line() + thm()
</code></pre>
<p><img src="assets/fig/wp6-1.png" title="plot of chunk wp6" alt="plot of chunk wp6" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-29" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Line chart and scatter plot</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(WP.df, aes(x=year, y=Asia)) +
geom_line(size=2) + #size控制線的寬度或點的大小
geom_point(size=5) + thm()
</code></pre>
<p><img src="assets/fig/wp7-1.png" title="plot of chunk wp7" alt="plot of chunk wp7" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-30" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>How to plot multiple lines?</h2>
</hgroup>
<article data-timings="">
<h3>Wide format</h3>
<!-- html table generated in R 3.2.2 by xtable 1.8-0 package -->
<!-- Thu Jan 14 02:48:11 2016 -->
<table border=1>
<tr> <th> </th> <th> N.Amer </th> <th> Europe </th> <th> Asia </th> <th> S.Amer </th> <th> Oceania </th> <th> Africa </th> <th> Mid.Amer </th> <th> year </th> </tr>
<tr> <td align="right"> 1951 </td> <td align="right"> 45939.00 </td> <td align="right"> 21574.00 </td> <td align="right"> 2876.00 </td> <td align="right"> 1815.00 </td> <td align="right"> 1646.00 </td> <td align="right"> 89.00 </td> <td align="right"> 555.00 </td> <td align="right"> 1951.00 </td> </tr>
<tr> <td align="right"> 1956 </td> <td align="right"> 60423.00 </td> <td align="right"> 29990.00 </td> <td align="right"> 4708.00 </td> <td align="right"> 2568.00 </td> <td align="right"> 2366.00 </td> <td align="right"> 1411.00 </td> <td align="right"> 733.00 </td> <td align="right"> 1956.00 </td> </tr>
<tr> <td align="right"> 1957 </td> <td align="right"> 64721.00 </td> <td align="right"> 32510.00 </td> <td align="right"> 5230.00 </td> <td align="right"> 2695.00 </td> <td align="right"> 2526.00 </td> <td align="right"> 1546.00 </td> <td align="right"> 773.00 </td> <td align="right"> 1957.00 </td> </tr>
<tr> <td align="right"> 1958 </td> <td align="right"> 68484.00 </td> <td align="right"> 35218.00 </td> <td align="right"> 6662.00 </td> <td align="right"> 2845.00 </td> <td align="right"> 2691.00 </td> <td align="right"> 1663.00 </td> <td align="right"> 836.00 </td> <td align="right"> 1958.00 </td> </tr>
<tr> <td align="right"> 1959 </td> <td align="right"> 71799.00 </td> <td align="right"> 37598.00 </td> <td align="right"> 6856.00 </td> <td align="right"> 3000.00 </td> <td align="right"> 2868.00 </td> <td align="right"> 1769.00 </td> <td align="right"> 911.00 </td> <td align="right"> 1959.00 </td> </tr>
<tr> <td align="right"> 1960 </td> <td align="right"> 76036.00 </td> <td align="right"> 40341.00 </td> <td align="right"> 8220.00 </td> <td align="right"> 3145.00 </td> <td align="right"> 3054.00 </td> <td align="right"> 1905.00 </td> <td align="right"> 1008.00 </td> <td align="right"> 1960.00 </td> </tr>
<tr> <td align="right"> 1961 </td> <td align="right"> 79831.00 </td> <td align="right"> 43173.00 </td> <td align="right"> 9053.00 </td> <td align="right"> 3338.00 </td> <td align="right"> 3224.00 </td> <td align="right"> 2005.00 </td> <td align="right"> 1076.00 </td> <td align="right"> 1961.00 </td> </tr>
</table>
<p>\[\Downarrow\]</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-31" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h3>Long format</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">library(reshape2)
WP.long <- melt(WP.df, id='year') #id是將保留的欄位名稱
colnames(WP.long) <- c('year','area','number')
</code></pre>
<!-- html table generated in R 3.2.2 by xtable 1.8-0 package -->
<!-- Thu Jan 14 02:48:11 2016 -->
<table border=1>
<tr> <th> </th> <th> year </th> <th> area </th> <th> number </th> </tr>
<tr> <td align="right"> 1 </td> <td align="right"> 1951.00 </td> <td> N.Amer </td> <td align="right"> 45939.00 </td> </tr>
<tr> <td align="right"> 2 </td> <td align="right"> 1956.00 </td> <td> N.Amer </td> <td align="right"> 60423.00 </td> </tr>
<tr> <td align="right"> 3 </td> <td align="right"> 1957.00 </td> <td> N.Amer </td> <td align="right"> 64721.00 </td> </tr>
<tr> <td align="right"> 4 </td> <td align="right"> 1958.00 </td> <td> N.Amer </td> <td align="right"> 68484.00 </td> </tr>
<tr> <td align="right"> 5 </td> <td align="right"> 1959.00 </td> <td> N.Amer </td> <td align="right"> 71799.00 </td> </tr>
<tr> <td align="right"> 6 </td> <td align="right"> 1960.00 </td> <td> N.Amer </td> <td align="right"> 76036.00 </td> </tr>
<tr> <td align="right"> 7 </td> <td align="right"> 1961.00 </td> <td> N.Amer </td> <td align="right"> 79831.00 </td> </tr>
<tr> <td align="right"> 8 </td> <td align="right"> 1951.00 </td> <td> Europe </td> <td align="right"> 21574.00 </td> </tr>
<tr> <td align="right"> 9 </td> <td align="right"> 1956.00 </td> <td> Europe </td> <td align="right"> 29990.00 </td> </tr>
<tr> <td align="right"> 10 </td> <td align="right"> 1957.00 </td> <td> Europe </td> <td align="right"> 32510.00 </td> </tr>
<tr> <td align="right"> 11 </td> <td align="right"> 1958.00 </td> <td> Europe </td> <td align="right"> 35218.00 </td> </tr>
<tr> <td align="right"> 12 </td> <td align="right"> 1959.00 </td> <td> Europe </td> <td align="right"> 37598.00 </td> </tr>
<tr> <td align="right"> 13 </td> <td align="right"> 1960.00 </td> <td> Europe </td> <td align="right"> 40341.00 </td> </tr>
<tr> <td align="right"> 14 </td> <td align="right"> 1961.00 </td> <td> Europe </td> <td align="right"> 43173.00 </td> </tr>
<tr> <td align="right"> 15 </td> <td align="right"> 1951.00 </td> <td> Asia </td> <td align="right"> 2876.00 </td> </tr>
<tr> <td align="right"> 16 </td> <td align="right"> 1956.00 </td> <td> Asia </td> <td align="right"> 4708.00 </td> </tr>
<tr> <td align="right"> 17 </td> <td align="right"> 1957.00 </td> <td> Asia </td> <td align="right"> 5230.00 </td> </tr>
<tr> <td align="right"> 18 </td> <td align="right"> 1958.00 </td> <td> Asia </td> <td align="right"> 6662.00 </td> </tr>
<tr> <td align="right"> 19 </td> <td align="right"> 1959.00 </td> <td> Asia </td> <td align="right"> 6856.00 </td> </tr>
<tr> <td align="right"> 20 </td> <td align="right"> 1960.00 </td> <td> Asia </td> <td align="right"> 8220.00 </td> </tr>
<tr> <td align="right"> 21 </td> <td align="right"> 1961.00 </td> <td> Asia </td> <td align="right"> 9053.00 </td> </tr>
<tr> <td align="right"> 22 </td> <td align="right"> 1951.00 </td> <td> S.Amer </td> <td align="right"> 1815.00 </td> </tr>
<tr> <td align="right"> 23 </td> <td align="right"> 1956.00 </td> <td> S.Amer </td> <td align="right"> 2568.00 </td> </tr>
<tr> <td align="right"> 24 </td> <td align="right"> 1957.00 </td> <td> S.Amer </td> <td align="right"> 2695.00 </td> </tr>
<tr> <td align="right"> 25 </td> <td align="right"> 1958.00 </td> <td> S.Amer </td> <td align="right"> 2845.00 </td> </tr>
<tr> <td align="right"> 26 </td> <td align="right"> 1959.00 </td> <td> S.Amer </td> <td align="right"> 3000.00 </td> </tr>
<tr> <td align="right"> 27 </td> <td align="right"> 1960.00 </td> <td> S.Amer </td> <td align="right"> 3145.00 </td> </tr>
<tr> <td align="right"> 28 </td> <td align="right"> 1961.00 </td> <td> S.Amer </td> <td align="right"> 3338.00 </td> </tr>
<tr> <td align="right"> 29 </td> <td align="right"> 1951.00 </td> <td> Oceania </td> <td align="right"> 1646.00 </td> </tr>
<tr> <td align="right"> 30 </td> <td align="right"> 1956.00 </td> <td> Oceania </td> <td align="right"> 2366.00 </td> </tr>
<tr> <td align="right"> 31 </td> <td align="right"> 1957.00 </td> <td> Oceania </td> <td align="right"> 2526.00 </td> </tr>
<tr> <td align="right"> 32 </td> <td align="right"> 1958.00 </td> <td> Oceania </td> <td align="right"> 2691.00 </td> </tr>
<tr> <td align="right"> 33 </td> <td align="right"> 1959.00 </td> <td> Oceania </td> <td align="right"> 2868.00 </td> </tr>
<tr> <td align="right"> 34 </td> <td align="right"> 1960.00 </td> <td> Oceania </td> <td align="right"> 3054.00 </td> </tr>
<tr> <td align="right"> 35 </td> <td align="right"> 1961.00 </td> <td> Oceania </td> <td align="right"> 3224.00 </td> </tr>
<tr> <td align="right"> 36 </td> <td align="right"> 1951.00 </td> <td> Africa </td> <td align="right"> 89.00 </td> </tr>
<tr> <td align="right"> 37 </td> <td align="right"> 1956.00 </td> <td> Africa </td> <td align="right"> 1411.00 </td> </tr>
<tr> <td align="right"> 38 </td> <td align="right"> 1957.00 </td> <td> Africa </td> <td align="right"> 1546.00 </td> </tr>
<tr> <td align="right"> 39 </td> <td align="right"> 1958.00 </td> <td> Africa </td> <td align="right"> 1663.00 </td> </tr>
<tr> <td align="right"> 40 </td> <td align="right"> 1959.00 </td> <td> Africa </td> <td align="right"> 1769.00 </td> </tr>
<tr> <td align="right"> 41 </td> <td align="right"> 1960.00 </td> <td> Africa </td> <td align="right"> 1905.00 </td> </tr>
<tr> <td align="right"> 42 </td> <td align="right"> 1961.00 </td> <td> Africa </td> <td align="right"> 2005.00 </td> </tr>
<tr> <td align="right"> 43 </td> <td align="right"> 1951.00 </td> <td> Mid.Amer </td> <td align="right"> 555.00 </td> </tr>
<tr> <td align="right"> 44 </td> <td align="right"> 1956.00 </td> <td> Mid.Amer </td> <td align="right"> 733.00 </td> </tr>
<tr> <td align="right"> 45 </td> <td align="right"> 1957.00 </td> <td> Mid.Amer </td> <td align="right"> 773.00 </td> </tr>
<tr> <td align="right"> 46 </td> <td align="right"> 1958.00 </td> <td> Mid.Amer </td> <td align="right"> 836.00 </td> </tr>
<tr> <td align="right"> 47 </td> <td align="right"> 1959.00 </td> <td> Mid.Amer </td> <td align="right"> 911.00 </td> </tr>
<tr> <td align="right"> 48 </td> <td align="right"> 1960.00 </td> <td> Mid.Amer </td> <td align="right"> 1008.00 </td> </tr>
<tr> <td align="right"> 49 </td> <td align="right"> 1961.00 </td> <td> Mid.Amer </td> <td align="right"> 1076.00 </td> </tr>
</table>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-32" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Multiple Line</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(WP.long,aes(x=year, y=number, group=area, color=area)) + # gruop按照不同區域劃線
geom_line(size=1.5) + geom_point(size=5) + thm()
</code></pre>
<p><img src="assets/fig/wp11-1.png" title="plot of chunk wp11" alt="plot of chunk wp11" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-33" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>質化 v.s. 量化:Bar Chart</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-34" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>讀取檔案</h2>
</hgroup>
<article data-timings="">
<p><a href="http://goo.gl/mwtvMj">痞客邦 Visitor Log 訓練資料集 (train.csv)</a></p>
<pre><code class="r">pixnet <- read.csv('train.csv', stringsAsFactors = FALSE)
</code></pre>
<ul>
<li>2014-11-01 至 2014-11-30 期間,10000 筆隨機取樣的台灣地區網站訪客的瀏覽紀錄</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-35" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>欄位說明</h2>
</hgroup>
<article data-timings="">
<ul>
<li>url_hash - 去識別後的部落格文章 url</li>
<li>resolution - 瀏覽裝置的螢幕解析度</li>
<li>browser - 瀏覽裝置的瀏覽器</li>
<li>os - 瀏覽裝置的作業系統</li>
<li>device_marketing - 瀏覽裝置的產品型號</li>
<li>device_brand - 瀏覽裝置的品牌名稱</li>
<li>cookie_pta - 去識別化的瀏覽者代碼</li>
<li>date - 瀏覽日期</li>
<li>author_id - 文章作者 ID 去識別碼</li>
<li>category_id - 文章分類</li>
<li>referrer_venue - 訪客來源(網域)</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-36" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Bar chart - <code>geom_bar()</code></h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(pixnet, aes(x=referrer_venue)) + geom_bar() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
thm()
</code></pre>
<p><img src="assets/fig/pix1-1.png" title="plot of chunk pix1" alt="plot of chunk pix1" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-37" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Bar chart</h2>
</hgroup>
<article data-timings="">
<ul>
<li>長條圖排序</li>
</ul>
<pre><code class="r">library(dplyr)
level <- table(pixnet$referrer_venue) %>% names
rank <- table(pixnet$referrer_venue) %>% order(decreasing = TRUE)
level
</code></pre>
<pre><code>[1] "7headlines" "direct/none" "facebook" "google" "other"
[6] "pixnet" "yahoo"
</code></pre>
<pre><code class="r">level[rank]
</code></pre>
<pre><code>[1] "pixnet" "google" "direct/none" "yahoo" "facebook"
[6] "other" "7headlines"
</code></pre>
<pre><code class="r">pixnet$referrer_venue <- factor(pixnet$referrer_venue, levels = level[rank])
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-38" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Bar chart</h2>
</hgroup>
<article data-timings="">
<ul>
<li>長條圖排序</li>
</ul>
<pre><code class="r">ggplot(pixnet, aes(x=referrer_venue)) + geom_bar() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
thm()
</code></pre>
<p><img src="assets/fig/unnamed-chunk-7-1.png" title="plot of chunk unnamed-chunk-7" alt="plot of chunk unnamed-chunk-7" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-39" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>兩種類別</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ub2 <- filter(ubike, sarea=='中和區',hour==8) %>%
mutate(is.rain=rainfall>1) %>%
mutate(is.rain=factor(is.rain, levels=c(FALSE, TRUE),
labels = c("晴天","雨天"))) %>%
select(date,avg.bemp, sna, is.rain, tot) %>%
group_by(sna, is.rain) %>%
summarise(use_rate=mean(avg.bemp/tot))
head(ub2)
</code></pre>
<pre><code>Source: local data frame [6 x 3]
Groups: sna [3]
sna is.rain use_rate
(fctr) (fctr) (dbl)
1 捷運永安市場站 晴天 0.7410044
2 捷運永安市場站 雨天 0.7827569
3 秀山國小 晴天 0.5095047
4 秀山國小 雨天 0.3444234
5 中和公園 晴天 0.6878744
6 中和公園 雨天 0.6147391
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-40" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>兩種類別</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(ub2, aes(x=sna, y=use_rate, fill=is.rain)) +
geom_bar(stat='identity') + # stat='identity'以表格的值做為bar的高度
theme(axis.text.x = element_text(angle = 90, hjust = 1)) + #控制字的方向
thm()
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-41" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>兩種類別: stack</h2>
</hgroup>
<article data-timings="">
<p><img src="assets/fig/ubar1-1.png" title="plot of chunk ubar1" alt="plot of chunk ubar1" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-42" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>兩種類別: dodge</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(ub2,aes(x=sna, y=use_rate, fill=is.rain)) +
geom_bar(stat='identity',position = 'dodge') + #dodge類別並排
theme(axis.text.x = element_text(angle = 90, hjust = 1)) + #控制字的方向
thm()
</code></pre>
<p><img src="assets/fig/ubar2-1.png" title="plot of chunk ubar2" alt="plot of chunk ubar2" style="display: block; margin: auto;" /></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-43" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Pie Chart: Bar chart變形</h2>
</hgroup>
<article data-timings="">
<h3>整理資料</h3>
<pre><code class="r">pix <- data.frame(table(pixnet$referrer_venue)) #table可以算個類別個數
colnames(pix) <- c('referrer','count')
pix[5,2] <- pix[5,2] + pix[1,2]
pix <- pix[-1,]
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-44" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Pie Chart: Bar Chart變形</h2>
</hgroup>
<article data-timings="">
<p><img src="assets/fig/pix3-1.png" alt="plot of chunk pix3"> </p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-45" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Pie Chart: Bar Chart變形</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">ggplot(pix,aes(x="", y=count, fill=referrer))+
geom_bar(stat='identity', width=1)+
coord_polar('y')+
geom_text(aes(y = count*0.5+ c(0, cumsum(count)[-length(count)]),
label = paste(round(count/sum(count),3)*100,'%',sep="")),
size=5)+
theme(axis.title.y=element_blank(),
axis.text.x=element_blank(),
panel.grid=element_blank()) + thm()
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-46" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Pie Chart: Bar Chart變形</h2>
</hgroup>
<article data-timings="">
<h3>整理資料</h3>
<pre><code class="r">pix <- arrange(pix, -count)
ggplot(pix,aes(x="", y=count, fill=referrer)) +
geom_bar(stat='identity',width=1) +
coord_polar('y') +
geom_text(aes(y = count*0.5 + c(0, cumsum(count)[-length(count)]),
label = paste(round(count/sum(count),3)*100,'%',sep="")),
size=5) +
theme(axis.title.y = element_blank(),
axis.text.x=element_blank(),
panel.grid=element_blank()) + thm()
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-47" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Pie Chart: Bar Chart變形</h2>
</hgroup>
<article data-timings="">
<h3>整理資料</h3>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="dark segue" id="slide-48" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>快速重播 + 進階繪圖</h2>
</hgroup>
<article data-timings="">
</article>
<!-- Presenter Notes -->
</slide>
<slide class="largecontent" id="slide-49" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>ggplot2基本架構</h2>
</hgroup>
<article data-timings="">
<ul>
<li>資料 (data) 和映射 (mapping)</li>
<li>幾何對象 (<code>geom</code>etric)</li>
<li>座標尺度 (<code>scale</code>)</li>
<li>統計轉換 (<code>stat</code>istics)</li>
<li>座標系統 (<code>coord</code>inante)</li>
<li>圖層 (layer)</li>
<li>刻面 (<code>facet</code>)</li>
<li>主題 (<code>theme</code>)</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-50" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2>Data and Mapping</h2>
</hgroup>
<article data-timings="">
<ul>
<li>data : <code>ggplot(data=..)</code></li>
<li>mapping : <code>aes(x=..., y=...)</code></li>
</ul>
<pre><code class="r">ggplot(data=WP.df) + geom_line(aes(x=year, y=Asia))
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-51" style="background-image: url('libraries/frameworks/io2012-dsp/images/dsp-page-white1100.jpg'); background-repeat:no-repeat; background-position:center; background-size: 100% 100%;">
<hgroup>
<h2><code>geom</code>etric</h2>
</hgroup>