-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_EDA_Visualization.html
More file actions
executable file
·1134 lines (877 loc) · 96.4 KB
/
Copy pathR_EDA_Visualization.html
File metadata and controls
executable file
·1134 lines (877 loc) · 96.4 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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<base target="_blank">
<script type="text/javascript">
var SLIDE_CONFIG = {
// Slide settings
settings: {
title: 'EDA with R',
subtitle: 'Data Visualization with <strong>ggplot2</strong>',
useBuilds: true,
usePrettify: true,
enableSlideAreas: true,
enableTouch: true,
},
// Author information
presenters: [
{
name: 'DSP作者群' ,
company: '',
gplus: '',
twitter: '',
www: '',
github: ''
},
]
};
</script>
<link href="R_EDA_Visualization_files/ioslides-13.5.1/fonts/fonts.css" rel="stylesheet" />
<link href="R_EDA_Visualization_files/ioslides-13.5.1/theme/css/default.css" rel="stylesheet" />
<link href="R_EDA_Visualization_files/ioslides-13.5.1/theme/css/phone.css" rel="stylesheet" />
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/modernizr.custom.45394.js"></script>
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/prettify/prettify.js"></script>
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/prettify/lang-r.js"></script>
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/prettify/lang-yaml.js"></script>
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/hammer.js"></script>
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/slide-controller.js"></script>
<script src="R_EDA_Visualization_files/ioslides-13.5.1/js/slide-deck.js"></script>
<script src="R_EDA_Visualization_files/htmlwidgets-1.3/htmlwidgets.js"></script>
<script src="R_EDA_Visualization_files/plotly-binding-4.9.0/plotly.js"></script>
<script src="R_EDA_Visualization_files/typedarray-0.1/typedarray.min.js"></script>
<script src="R_EDA_Visualization_files/jquery-1.11.3/jquery.min.js"></script>
<link href="R_EDA_Visualization_files/crosstalk-1.0.0/css/crosstalk.css" rel="stylesheet" />
<script src="R_EDA_Visualization_files/crosstalk-1.0.0/js/crosstalk.min.js"></script>
<link href="R_EDA_Visualization_files/plotly-htmlwidgets-css-1.46.1/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="R_EDA_Visualization_files/plotly-main-1.46.1/plotly-latest.min.js"></script>
<style type="text/css">
b, strong {
font-weight: bold;
}
em {
font-style: italic;
}
summary {
display: list-item;
}
slides > slide {
-webkit-transition: all 0.4s ease-in-out;
-moz-transition: all 0.4s ease-in-out;
-o-transition: all 0.4s ease-in-out;
transition: all 0.4s ease-in-out;
}
.auto-fadein {
-webkit-transition: opacity 0.6s ease-in;
-webkit-transition-delay: 0.4s;
-moz-transition: opacity 0.6s ease-in 0.4s;
-o-transition: opacity 0.6s ease-in 0.4s;
transition: opacity 0.6s ease-in 0.4s;
opacity: 0;
}
</style>
<link rel="stylesheet" href="css/dsp.css" type="text/css" />
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="title-slide segue nobackground">
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
<p style="margin-top: 6px; margin-left: -2px;">2019-05</p>
</hgroup>
</slide>
<slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>課程綱要</h2></hgroup><article >
</article></slide><slide class=""><hgroup><h2>Agenda</h2></hgroup><article id="agenda">
<div style="float:left;width:48%;">
<ul>
<li>Data Visualization</li>
<li><code>ggplot2</code> in R
<ul>
<li>基本架構介紹</li>
<li>起手式(基本語法)</li>
<li>應用(各種圖形的呈現)
<ul>
<li><code>Bar</code></li>
<li><code>Line</code></li>
<li><code>Histogram</code></li>
<li><code>Point</code></li>
<li><code>Heatmap</code></li>
</ul></li>
<li>進階技巧</li>
</ul></li>
</ul></div>
<div style="float:left;width:48%;">
<ul>
<li>互動式視覺化呈現</li>
<li>Bonus</li>
</ul>
<p><img src='img/a2.jpg' style='height: 350px'></img></p></div>
</article></slide><slide class=""><hgroup><h2>Data Visualization</h2></hgroup><article id="data-visualization">
<div style="float:left;width:48%;">
<ul>
<li>清晰有效地傳達與溝通訊息</li>
<li>教學、研究、宣傳</li>
<li>美學、功能兼顧</li>
<li>統計圖形、訊息可視化</li>
<li>一張好圖,勝過千言萬語</li>
</ul></div>
<div style="float:left;width:48%;">
<p><img src='img/w2.png' style='height: 450px'></img></p></div>
</article></slide><slide class=""><hgroup><h2>ggplot2 簡介</h2></hgroup><article id="ggplot2-">
<div style="float:left;width:50%;">
<ul>
<li>套件下載次數第一名(<a href='https://www.rdocumentation.org/trends?page1=1&sort1=direct&page2=1&sort2=total&page3=1&page4=1' title=''>Source</a>)</li>
<li>R環境下的繪圖套件</li>
<li>取自 “The Grammar of Graphics” (Leland Wilkinson, 2005)</li>
<li><a href='https://github.com/cosname/ggplot2-translation/blob/master/preface.md' title=''>設計理念</a>
<ul>
<li>採用圖層系統</li>
<li>用抽象的概念來控制圖形,</br> 避免細節繁瑣</li>
<li>圖形美觀</li>
</ul></li>
</ul></div>
<div style="float:right;width:48%;">
<p><img src='img/most_download_in_R.png' width=420 align='center'></img></p></div>
</article></slide><slide class=""><hgroup><h2>The Anatomy of a Plot</h2></hgroup><article id="the-anatomy-of-a-plot">
<center>
<img src='img/anatomy.png' width=750 align='center'></img>
</center>
</article></slide><slide class=""><hgroup><h2>ggplot2 基本架構</h2></hgroup><article id="ggplot2-">
<ul>
<li>資料 (data) 和映射 (mapping)</li>
<li>美學對應(<code>aes</code>thetic)</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></slide><slide class=""><hgroup><h2>ggplot2 基本架構(2)</h2></hgroup><article id="ggplot2-2">
<center>
<img src='img/ggplot.jpeg' width=550 align='center'></img> </br> source: <a href='http://goo.gl/Odt2Rs' title=''>http://goo.gl/Odt2Rs</a>
</center>
</article></slide><slide class=""><hgroup><h2>ggplot2 基本語法</h2></hgroup><article id="ggplot2-">
<pre class = 'prettyprint lang-r'>ggplot(data=..., aes(x=..., y=...)) +
geom_xxx(...) +
stat_xxx(...) +
facet_xxx(...) + ...</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_line</code>, <code>geom_points</code>, …</li>
</ul>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>圖層概念的實作</h2></hgroup><article >
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>前菜 - ggplot2到底有多少種圖</h2></hgroup><article id="---ggplot2">
</article></slide><slide class=""><hgroup><h2>Various functions</h2></hgroup><article id="various-functions">
<pre class = 'prettyprint lang-r'>library(ggplot2)
# list all geom
ls(pattern = '^geom_', env = as.environment('package:ggplot2'))</pre>
<pre > [1] "geom_abline" "geom_area" "geom_bar"
[4] "geom_bin2d" "geom_blank" "geom_boxplot"
[7] "geom_col" "geom_contour" "geom_count"
[10] "geom_crossbar" "geom_curve" "geom_density"
[13] "geom_density_2d" "geom_density2d" "geom_dotplot"
[16] "geom_errorbar" "geom_errorbarh" "geom_freqpoly"
[19] "geom_hex" "geom_histogram" "geom_hline"
[22] "geom_jitter" "geom_label" "geom_line"
[25] "geom_linerange" "geom_map" "geom_path"
[28] "geom_point" "geom_pointrange" "geom_polygon"
[31] "geom_qq" "geom_qq_line" "geom_quantile"
[34] "geom_raster" "geom_rect" "geom_ribbon"
[37] "geom_rug" "geom_segment" "geom_sf"
[40] "geom_sf_label" "geom_sf_text" "geom_smooth"
[43] "geom_spoke" "geom_step" "geom_text"
[46] "geom_tile" "geom_violin" "geom_vline" </pre>
</article></slide><slide class=""><hgroup><h2><strong>注意</strong></h2></hgroup><article >
<ul>
<li>使用 <code>data.frame</code> 儲存資料 (不可以丟 matrix 物件)</li>
<li>使用 <code>wide format</code> to <code>long format</code></li>
</ul>
<p></br></p>
<center>
<img src='img/wide-long.png' title=''/>
</center>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>正片開始</h2></hgroup><article >
</article></slide><slide class=""><hgroup><h2>首先請先安裝以下套件</h2></hgroup><article >
<ul>
<li>安裝套件</li>
</ul>
<pre class = 'prettyprint lang-r'>install.packages(c("tidyr","dplyr","ggplot2"))</pre>
<ul>
<li>載入套件 (<strong>注意</strong>:下載完套件一定要記得 <code>library</code> 才能使用喲!)</li>
</ul>
<pre class = 'prettyprint lang-r'>library(ggplot2)
library(dplyr)
library(tidyr)</pre>
<p><code>or</code></p>
<ul>
<li>安裝套件</li>
</ul>
<pre class = 'prettyprint lang-r'>install.packages("tidyverse")</pre>
<ul>
<li>載入套件 (<strong>注意</strong>:下載完套件一定要記得 <code>library</code> 才能使用喲!)</li>
</ul>
<pre class = 'prettyprint lang-r'>library(tidyverse)</pre>
</article></slide><slide class=""><hgroup><h2>一切從讀檔開始(CSV)</h2></hgroup><article id="csv">
<ul>
<li><a href='https://ling32342.github.io/2017_NCCU_R/data/transaction.csv' title=''>課程資料下載</a></li>
</ul>
<pre class = 'prettyprint lang-r'>############### 相對路徑 ###############
# 瞭解現在我們所處在的路徑
getwd()
# 設定我們檔案存放的路徑
setwd()
# 讀檔起手式
data <- read.csv("transaction.csv")
# 若讀入的是亂碼,試試以下
data <- read.csv("transaction.csv",fileEncoding = 'big5') #如果你是mac
data <- read.csv("transaction.csv",fileEncoding = 'utf-8') #如果你是windows</pre>
</article></slide><slide class=""><hgroup><h2>資料介紹</h2></hgroup><article >
<table class = 'rmdtable'>
<tr class="header">
<th align="right">area_land</th>
<th align="right">price_total</th>
<th align="left">build_type</th>
<th align="right">area_park</th>
<th align="right">area_build</th>
<th align="right">trac_year</th>
</tr>
<tr class="odd">
<td align="right">35.0</td>
<td align="right">6380000</td>
<td align="left">公寓(5樓含以下無電梯)</td>
<td align="right">0.0</td>
<td align="right">61</td>
<td align="right">102</td>
</tr>
<tr class="even">
<td align="right">10.7</td>
<td align="right">12010000</td>
<td align="left">住宅大樓(11層含以上有電梯)</td>
<td align="right">0.0</td>
<td align="right">104</td>
<td align="right">102</td>
</tr>
<tr class="odd">
<td align="right">8.5</td>
<td align="right">10080000</td>
<td align="left">套房(1房1廳1衛)</td>
<td align="right">8.6</td>
<td align="right">52</td>
<td align="right">102</td>
</tr>
<tr class="even">
<td align="right">4.7</td>
<td align="right">4600000</td>
<td align="left">住宅大樓(11層含以上有電梯)</td>
<td align="right">0.0</td>
<td align="right">39</td>
<td align="right">102</td>
</tr>
<tr class="odd">
<td align="right">31.0</td>
<td align="right">23800000</td>
<td align="left">華廈(10層含以下有電梯)</td>
<td align="right">0.0</td>
<td align="right">185</td>
<td align="right">102</td>
</tr>
</table>
</article></slide><slide class=""><hgroup><h2>欄位說明</h2></hgroup><article >
<div style="float:left;width:48%;">
<table class = 'rmdtable'>
<tr class="header">
<th align="left">英文欄位名稱</th>
<th align="left">中文欄位名稱</th>
</tr>
<tr class="odd">
<td align="left">city</td>
<td align="left">縣市</td>
</tr>
<tr class="even">
<td align="left">district</td>
<td align="left">鄉鎮市區</td>
</tr>
<tr class="odd">
<td align="left">trac_year</td>
<td align="left">交易年份</td>
</tr>
<tr class="even">
<td align="left">trac_month</td>
<td align="left">交易月份</td>
</tr>
<tr class="odd">
<td align="left">trac_type</td>
<td align="left">交易標的</td>
</tr>
<tr class="even">
<td align="left">trac_content</td>
<td align="left">交易筆棟數</td>
</tr>
<tr class="odd">
<td align="left">use_type</td>
<td align="left">使用分區或編定</td>
</tr>
</table></div>
<div style="float:right;width:50%;">
<table class = 'rmdtable'>
<tr class="header">
<th align="left">英文欄位名稱</th>
<th align="left">中文欄位名稱</th>
</tr>
<tr class="odd">
<td align="left">build_type</td>
<td align="left">建物型態</td>
</tr>
<tr class="even">
<td align="left">build_ymd</td>
<td align="left">建築完成年月</td>
</tr>
<tr class="odd">
<td align="left">area_land</td>
<td align="left">土地移轉總面積.平方公尺.</td>
</tr>
<tr class="even">
<td align="left">area_build</td>
<td align="left">建物移轉總面積.平方公尺.</td>
</tr>
<tr class="odd">
<td align="left">area_park</td>
<td align="left">車位移轉總面積.平方公尺.</td>
</tr>
<tr class="even">
<td align="left">price_total</td>
<td align="left">總價.元.</td>
</tr>
<tr class="odd">
<td align="left">price_unit</td>
<td align="left">單價.元.平方公尺.</td>
</tr>
</table></div>
</article></slide><slide class=""><hgroup><h2>以為開始了嗎?</h2></hgroup><article >
<ul>
<li>進行分析前,先去了解資料的型態與特性</li>
</ul>
<pre class = 'prettyprint lang-r'>str(data)</pre>
<pre >'data.frame': 153598 obs. of 15 variables:
$ city : Factor w/ 4 levels "高雄市","臺北市",..: 2 2 2 2 2 2 2 2 2 2 ...
$ district : Factor w/ 99 levels "阿蓮區","八里區",..: 63 96 96 5 96 5 5 38 38 42 ...
$ trac_year : int 102 102 102 102 102 102 102 102 102 102 ...
$ trac_month : Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ trac_type : Factor w/ 2 levels "房地(土地+建物)",..: 1 1 2 1 1 2 2 2 2 2 ...
$ trac_content: Factor w/ 327 levels "土地0建物0車位0",..: 57 57 58 57 57 62 62 58 64 167 ...
$ use_type : Factor w/ 5 levels "工","農","其他",..: 5 4 4 4 5 5 5 5 1 5 ...
$ build_type : Factor w/ 12 levels "辦公商業大樓",..: 6 12 10 12 7 7 7 12 1 12 ...
$ build_ymd : int 701109 701228 970114 851218 970624 1010724 1010724 1000414 1010531 870910 ...
$ area_land : num 34.96 10.71 8.51 4.7 30.97 ...
$ area_build : num 60.6 104.5 51.9 39.4 185.2 ...
$ area_park : num 0 0 8.55 0 0 ...
$ price_total : num 6380000 12010000 10080000 4600000 23800000 ...
$ price_unit : int 105263 114928 194070 116900 128510 218147 204716 174613 133648 27658 ...
$ age : int 32 32 5 17 5 1 1 2 1 15 ...</pre>
</article></slide><slide class=""><hgroup><h2>身為資料分析師,一定要有的好習慣!</h2></hgroup><article >
<ul>
<li>暸解基本的各變數統計量值</li>
</ul>
<pre class = 'prettyprint lang-r'>summary(data) </pre>
<pre > city district trac_year trac_month
高雄市:34460 淡水區 : 7172 Min. :102 12 :15206
臺北市:24238 西屯區 : 5974 1st Qu.:102 5 :15079
臺中市:37482 新莊區 : 5955 Median :102 4 :14682
新北市:57418 北屯區 : 5881 Mean :102 3 :14523
新店區 : 5873 3rd Qu.:102 7 :13805
中和區 : 5719 Max. :102 6 :13392
(Other):117024 (Other):66911
trac_type trac_content use_type
房地(土地+建物) :91613 土地1建物1車位0:66792 工 : 3233
房地(土地+建物)+車位:61985 土地1建物1車位1:41031 農 : 577
土地2建物1車位0:14537 其他: 8206
土地1建物1車位2: 7195 商 : 26205
土地2建物1車位1: 4787 住 :115377
土地3建物1車位0: 4691
(Other) :14565
build_type build_ymd area_land
住宅大樓(11層含以上有電梯):70725 Min. : 100602 Min. : 0
公寓(5樓含以下無電梯) :23211 1st Qu.: 780326 1st Qu.: 13
透天厝 :21954 Median : 870506 Median : 22
華廈(10層含以下有電梯) :20365 Mean : 868754 Mean : 42
套房(1房1廳1衛) : 9709 3rd Qu.: 991201 3rd Qu.: 36
店面(店鋪) : 2888 Max. :1030313 Max. :127088
(Other) : 4746
area_build area_park price_total price_unit
Min. : 0 Min. : 0 Min. : 0 Min. : 0
1st Qu.: 85 1st Qu.: 0 1st Qu.: 4900000 1st Qu.: 42685
Median : 124 Median : 0 Median : 8400000 Median : 67880
Mean : 153 Mean : 25 Mean : 12879580 Mean : 86176
3rd Qu.: 179 3rd Qu.: 9 3rd Qu.: 14580000 3rd Qu.: 111173
Max. :79669 Max. :2400000 Max. :8800000000 Max. :4284119
NA's :461
age
Min. :-1
1st Qu.: 3
Median :15
Mean :15
3rd Qu.:24
Max. :92
</pre>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>質化 v.s. 量化:Bar Chart</h2></hgroup><article id="-v.s.-bar-chart">
</article></slide><slide class=""><hgroup><h2><code>geom_bar</code></h2></hgroup><article id="geom_bar">
<ul>
<li>先來<code>比較</code>看看2013年在各縣市的案件交易量</li>
<li>SimHei字體<a href='http://www.fontpalace.com/font-details/SimHei/' title=''>下載連結</a></li>
</ul>
<pre class = 'prettyprint lang-r'>thm <- function() theme(text = element_text(size = 15, family = "SimHei")) # 控制字體與大小
# SimHei是只有Mac才有的字體, 用來解決Mac系統中文顯示錯誤的問題
# Windows系統使用者請忽略 `+ thm()` 指令
data %>%
ggplot(aes(x = city)) + geom_bar(stat = "count") + thm() # stat = "count" 算個數</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-16-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Change labels !</h2></hgroup><article id="change-labels">
<pre class = 'prettyprint lang-r'>data %>%
ggplot(aes(x = city)) + geom_bar(stat = "count") + thm() +
labs(title = "各縣市交易量", x = "縣市", y = "交易量") # lab用來幫圖形的標題、x軸與y軸做命名</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-17-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Change colors !</h2></hgroup><article id="change-colors">
<ul>
<li>顏色調整:<code>color</code> vs <code>fill</code>?</li>
</ul>
<pre class = 'prettyprint lang-r'>data %>%
ggplot(aes(x = city)) + geom_bar(stat = "count") + thm() +
labs(title = "各縣市交易量", x = "縣市", y = "交易量") +
geom_bar(fill = 'snow', color = 'black') # see colors() if you're picky</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-18-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Bar chart</h2></hgroup><article id="bar-chart">
<ul>
<li><code>比較</code>臺北市各行政區的案件交易量</li>
</ul>
<pre class = 'prettyprint lang-r'># 資料整理
table = data %>%
filter(city == "臺北市") %>%
group_by(district) %>%
summarise(Count = n()) # dplyr::n 用來計數
table</pre>
<pre ># A tibble: 12 x 2
district Count
<fct> <int>
1 北投區 2416
2 大安區 1755
3 大同區 1092
4 南港區 989
5 內湖區 3598
6 士林區 1859
7 松山區 1556
8 萬華區 1584
9 文山區 2197
10 信義區 1585
11 中山區 4410
12 中正區 1197</pre>
</article></slide><slide class=""><hgroup><h2>Bar chart</h2></hgroup><article id="bar-chart-1">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = district, y = Count)) +
geom_bar(stat = "identity") + thm() # stat='identity'以表格的值做為bar的高度</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-20-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Reoder x</h2></hgroup><article id="reoder-x">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = reorder(district, -Count), y = Count)) +
geom_bar(stat = 'identity') + thm() +
labs(titles = "臺北市各行政區交易量", x = "行政區", y = "Count")</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-21-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>小挑戰</h2></hgroup><article >
<ul>
<li>計算並<code>比較</code>臺北市&高雄市的各使用分區或編定(<code>use_type</code>)所佔比例</li>
</ul>
<pre ># A tibble: 10 x 4
# Groups: city [2]
city use_type Count rate
<fct> <fct> <int> <dbl>
1 高雄市 工 196 0.01
2 高雄市 農 123 0
3 高雄市 其他 532 0.02
4 高雄市 商 7582 0.22
5 高雄市 住 26027 0.76
6 臺北市 工 442 0.02
7 臺北市 農 3 0
8 臺北市 其他 1709 0.07
9 臺北市 商 7323 0.3
10 臺北市 住 14761 0.61</pre>
</article></slide><slide class=""><hgroup><h2>參考解答</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>table = data %>%
filter(city == "臺北市" | city == "高雄市" ) %>%
group_by(city, use_type) %>%
summarise(Count = n()) %>%
mutate(rate = round(Count/sum(Count), 2)) </pre>
<center>
<img src='img/white2.png' width=600 align='center'></img>
</center>
</article></slide><slide class=""><hgroup><h2>Grouping:<code>stack</code></h2></hgroup><article id="groupingstack">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = city, y = Count, fill = use_type)) +
geom_bar(stat = 'identity', position = 'stack') + thm() # stack類別堆疊</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-24-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Grouping:<code>dodge</code></h2></hgroup><article id="groupingdodge">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = city, y = rate, fill = use_type)) +
geom_bar(stat = 'identity', position = 'dodge') + # dodge類別並排
thm() + scale_fill_discrete(name ="使用分區或編定") # 設定圖例的顯示</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-25-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>量化 v.s. 量化:Line Chart</h2></hgroup><article id="-v.s.-line-chart">
</article></slide><slide class=""><hgroup><h2><code>geom_line</code></h2></hgroup><article id="geom_line">
<ul>
<li>各月份交易量</li>
</ul>
<pre class = 'prettyprint lang-r'>table = data %>%
group_by(trac_month) %>%
summarise(Count=n())
table</pre>
<pre ># A tibble: 12 x 2
trac_month Count
<fct> <int>
1 1 10367
2 2 7871
3 3 14523
4 4 14682
5 5 15079
6 6 13392
7 7 13805
8 8 11714
9 9 10814
10 10 13166
11 11 12979
12 12 15206</pre>
</article></slide><slide class=""><hgroup><h2>Line chart</h2></hgroup><article id="line-chart">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = trac_month, y = Count, group = 1)) +
geom_line() + thm() + labs(x = "交易月份" , y = "交易數量")</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-27-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2><code>Multiple</code> Line</h2></hgroup><article id="multiple-line">
<ul>
<li>各縣市各月份交易量比較</li>
</ul>
<pre class = 'prettyprint lang-r'>table = data %>%
group_by(city, trac_month) %>%
summarise(Count = n())
table</pre>
<pre ># A tibble: 48 x 3
# Groups: city [?]
city trac_month Count
<fct> <fct> <int>
1 高雄市 1 2511
2 高雄市 2 1897
3 高雄市 3 3355
4 高雄市 4 3524
5 高雄市 5 3378
6 高雄市 6 3097
7 高雄市 7 3063
8 高雄市 8 2573
9 高雄市 9 2354
10 高雄市 10 2590
# ... with 38 more rows</pre>
</article></slide><slide class=""><hgroup><h2><code>Multiple</code> Line</h2></hgroup><article id="multiple-line-1">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = trac_month, y = Count, group = city, color = city)) +
geom_line() + geom_point() + thm() +
labs(x = "交易月份" , y = "交易數量")</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-29-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>小挑戰</h2></hgroup><article id="-1">
<ul>
<li>計算臺北市&高雄市不同屋齡的交易量,並畫出 <code>Multiple</code> Line plot</li>
<li>如下圖:</li>
</ul>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-30-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>參考答案 (有發現什麼問題嗎?!)</h2></hgroup><article id="-">
<pre class = 'prettyprint lang-r'>data %>%
filter(city == "臺北市" | city == "高雄市" ) %>%
group_by(city, age) %>%
summarise(Count = n()) %>%
ggplot(aes(x = age, y = Count, group = city, color = city)) +
geom_line() + geom_point() + thm()</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-31-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>調整 x 軸的 scale 再看一次</h2></hgroup><article id="-x--scale-">
<ul>
<li>EDA的價值之一就是找出資料中的<code>不合理</code></li>
</ul>
<pre class = 'prettyprint lang-r'>data %>%
filter(city == "臺北市" | city == "高雄市" ) %>%
group_by(city, age) %>%
summarise(Count = n()) %>%
ggplot(aes(x = age, y = Count, group = city, color = city)) +
geom_line() + geom_point() + thm() + coord_cartesian(xlim = c(-3, 10))</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-32-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>單一數值:Histogram</h2></hgroup><article id="histogram">
</article></slide><slide class=""><hgroup><h2><code>geom_histogram</code></h2></hgroup><article id="geom_histogram">
<pre class = 'prettyprint lang-r'>data %>%
ggplot(aes(x = age, y =..count..)) +
geom_histogram()</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-33-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Histogram</h2></hgroup><article id="histogram-1">
<ul>
<li><code>aes(y=..count..)</code> vs. <code>aes(y=..density..)</code></li>
</ul>
<pre class = 'prettyprint lang-r'>data %>%
ggplot(aes(x = age, y =..density..)) +
geom_histogram()</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-34-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Histogram</h2></hgroup><article id="histogram-2">
<pre class = 'prettyprint lang-r'>data %>%
ggplot(aes(x = age, y =..density.., fill =..count..)) + # fill 依指定欄位填色
geom_histogram(binwidth = .5) </pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-35-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>Histogram + density</h2></hgroup><article id="histogram-density">
<ul>
<li><code>geom_histogram()</code> + <code>geom_density()</code></li>
</ul>
<pre class = 'prettyprint lang-r'>data %>%
ggplot(aes(x = age, y = ..density..)) +
geom_histogram(color = "black", fill = "white") +
geom_density(alpha = .2, fill = "#FF6666") # alpha設定透明度</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-36-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>量化 v.s. 量化:Scatter Plot</h2></hgroup><article id="-v.s.-scatter-plot">
</article></slide><slide class=""><hgroup><h2>Scatter plot</h2></hgroup><article id="scatter-plot">
<ul>
<li><code>geom_point</code></li>
</ul>
<pre class = 'prettyprint lang-r'>iris %>%
ggplot(aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
geom_point(shape = 1, size = 2) # shape控制圖示;size控制點的大小</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-37-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>point shape types in <code>ggplot2</code></h2></hgroup><article id="point-shape-types-in-ggplot2">
<center>
<img src='img/point_shape_types.png' style='height: 500px' align="middle"></img>
</center>
</article></slide><slide class=""><hgroup><h2>Scatter plot</h2></hgroup><article id="scatter-plot-1">
<ul>
<li>參數放在 aes() 函數裡面,表示資料依據指定欄位內容做不同的shape/size變化</li>
</ul>
<pre class = 'prettyprint lang-r'>iris %>%
ggplot(aes(x = Sepal.Length, y = Petal.Length, color = Species, shape = Species, size = Species)) +
geom_point() + scale_shape_manual(values = c(1,5,7)) + # 控制 shape 顯示圖示
scale_size_manual(values = c(1,2,3)) # 控制圖示 size 顯示大小</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-38-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>插播</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>install.packages("devtools")</pre>
<center>
<img src='img/devtools.png' width=750 align='center'></img>
</center>
<ul>
<li><a href='https://github.com/cttobin/ggthemr' title=''>配色懶人包ggthemr</a></li>
</ul>
</article></slide><slide class=""><hgroup><h2>小挑戰</h2></hgroup><article id="-2">
<ul>
<li>士林區,文山區,信義區三個區域的<strong>套房(1房1廳1衛)</strong>在屋齡與每平方公尺單價存在著怎樣的特性?</li>
</ul>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-40-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>參考答案</h2></hgroup><article >
<pre class = 'prettyprint lang-r'>library(ggthemr)
ggthemr('flat')
ans = data %>% filter(district == "文山區" | district == "士林區" |district == "信義區") %>% filter(build_type == "套房(1房1廳1衛)") %>% ggplot(aes(x = age, y = price_unit, color = district)) + geom_point(position = "jitter") + thm()
ans</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-41-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>HeatMap</h2></hgroup><article id="heatmap">
</article></slide><slide class=""><hgroup><h2>相關性熱圖</h2></hgroup><article >
<ul>
<li>通常我們會好奇變數與變數之間是不是存在著相關性</li>
<li>在統計學上有一種簡單的方式去計算變數間的關係指標,我們稱之為相關係數係數!</li>
<li>以下將以 iris 資料做為示範,用熱圖來呈現</li>
</ul>
<pre class = 'prettyprint lang-r'># 挑出前四欄位
dat <- iris %>%
select(1:4)
dat %>%
head()</pre>
<pre > Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4</pre>
</article></slide><slide class=""><hgroup><h2><code>寬轉長</code> 計算相關性</h2></hgroup><article id="-">
<pre class = 'prettyprint lang-r'>p = dat %>%
cor() %>%
as.data.frame() %>%
mutate(names = row.names(.)) %>%
gather(class, cor, 1:4)
p %>% head(10)</pre>
<pre > names class cor
1 Sepal.Length Sepal.Length 1.00
2 Sepal.Width Sepal.Length -0.12
3 Petal.Length Sepal.Length 0.87
4 Petal.Width Sepal.Length 0.82
5 Sepal.Length Sepal.Width -0.12
6 Sepal.Width Sepal.Width 1.00
7 Petal.Length Sepal.Width -0.43
8 Petal.Width Sepal.Width -0.37
9 Sepal.Length Petal.Length 0.87
10 Sepal.Width Petal.Length -0.43</pre>
</article></slide><slide class=""><hgroup><h2>Heatmap的好處</h2></hgroup><article id="heatmap">
<ul>
<li>藉由 Heatmap ,快速地看出變數之間的關聯程度</li>
</ul>
<pre class = 'prettyprint lang-r'>p %>%
ggplot(aes(x = names, y = class, fill= cor)) +
geom_tile() + labs(x = "", y = "") +
scale_fill_gradient2(limits = c(-1, 1))</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-44-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>進階技巧</h2></hgroup><article >
</article></slide><slide class=""><hgroup><h2><code>stat</code>istics</h2></hgroup><article id="statistics">
<ul>
<li>Fit regression line</li>
</ul>
<pre class = 'prettyprint lang-r'>library(ggthemr)
ggthemr('flat')
iris %>%
ggplot(aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
stat_smooth(method = lm, level = .95) # add se=FALSE to disable CI </pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-45-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2><code>facet</code></h2></hgroup><article id="facet">
<ul>
<li>比較各縣市在各月份下的交易量</li>
</ul>
<pre class = 'prettyprint lang-r'>table <- data %>%
group_by(city, trac_month) %>% # 選擇縣市、交易月份作為分群
summarise(total = n()) # 計算分群下的總數
table %>%
ggplot(aes(x = trac_month, y = total ,fill = city))+
geom_bar(stat = 'identity') + thm()</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-46-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2><code>facet</code></h2></hgroup><article id="facet-1">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = trac_month, y = total ,fill = city))+
geom_bar(stat = 'identity') + thm() +
facet_wrap( ~city , nrow = 2) +
labs(x = "交易月份", y="交易量")</pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-47-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2><code>coord</code>_flip()</h2></hgroup><article id="coord_flip">
<pre class = 'prettyprint lang-r'>table %>%
ggplot(aes(x = trac_month, y = total, fill = city)) +
geom_bar(stat = 'identity') + thm() + facet_wrap( ~ city, nrow = 2)+
labs(x = "交易月份", y = "交易量") + coord_flip() </pre>
<p><img src="R_EDA_Visualization_files/figure-html/unnamed-chunk-48-1.png" width="720" style="display: block; margin: auto;" /></p>
</article></slide><slide class=""><hgroup><h2>圖形輸出</h2></hgroup><article >
<ul>
<li>利用 RStudio UI 介面存擋</li>
<li>畫完圖之後,再存檔</li>
</ul>
<pre class = 'prettyprint lang-r'>ggsave('檔案名稱')
ggsave("plot.pdf", width = 4, height = 4)
ggsave("plot.png", width = 4, height = 4, dpi = 300)</pre>
</article></slide><slide class="segue dark nobackground level1"><hgroup class = 'auto-fadein'><h2>互動式視覺化呈現</h2></hgroup><article >
</article></slide><slide class=""><hgroup><h2>package:<code>Plotly</code></h2></hgroup><article id="packageplotly">
<ul>
<li>Plotly是一個資料視覺化的R套件,以簡單的方式,讓資料能夠產生互動的效果。</li>
<li>提供一個合作平台,使用者能夠將自己在R中繪製的圖存上屬於自己的Plotly平台上。</li>
<li><a href='https://plot.ly/' title=''>Plotly官方網站</a></li>
<li>結合了各式各樣的API,包裝<code>Python</code>、<code>R</code>、<code>Malab</code>、…等等</li>
</ul>
<ul class = 'build'>