2017.05.17
ggplot2
in R
Bar
Line
Histogram
Boxplot
point
geom
etric)scale
)stat
istics)coord
inante)facet
)theme
)ggplot(data=..., aes(x=..., y=...)) + geom_xxx(...) + stat_xxx(...) + facet_xxx(...) + ...
ggplot
描述 data 從哪來aes
描述圖上的元素跟 data 之類的對應關係geom_xxx
描述要畫圖的類型及相關調整的參數geom_bar
, geom_line
, geom_points
, …library(ggplot2) # list all geom ls(pattern = '^geom_', env = as.environment('package:ggplot2'))
[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_quantile" "geom_raster" [34] "geom_rect" "geom_ribbon" "geom_rug" [37] "geom_segment" "geom_smooth" "geom_spoke" [40] "geom_step" "geom_text" "geom_tile" [43] "geom_violin" "geom_vline"
data.frame
儲存資料 (不可以丟 matrix 物件)long format
(利用reshape2將資料轉換成 1 row = 1 observation)# 先定義一個資料表 data <- data.frame(身份 = c("大學部","研究所","教授","講師","助教"), 人數 = c(20,15,5,2,3)) # plot ggplot(data = data, aes(x = 身份, y = 人數)) + geom_bar(stat = "identity") + theme_grey(base_family = "STHeiti")
install.packages(c("ggplot2","dplyr","reshape2"),repos="http://cran.csie.ntu.edu.tw/")
library(ggplot2) library(dplyr) library(reshape2)
注意:下載完套件一定要記得 library
才能使用喲!
############### 絕對路徑 ############### # 請輸入完整的檔案路徑 data <- read.csv("/Users/hsiaoling/Desktop/data/transaction.csv") #如果你是mac data <- read.csv("C:\\Users\\transaction.csv") #如果你是windows ############### 相對路徑 ############### # 瞭解現在我們所處在的路徑 getwd() # 設定我們檔案存放的路徑 setwd() # 讀檔起手式 data <- read.csv("transaction.csv") # 若讀入的是亂碼,試試以下 data <- read.csv("transaction.csv",fileEncoding = 'big5') #如果你是mac data <- read.csv("transaction.csv",fileEncoding = 'utf-8') #如果你是windows
city district trac_year trac_month trac_type 1 臺北市 士林區 102 1 房地(土地+建物) 2 臺北市 中山區 102 1 房地(土地+建物) 3 臺北市 中山區 102 1 房地(土地+建物)+車位 4 臺北市 北投區 102 1 房地(土地+建物) 5 臺北市 中山區 102 1 房地(土地+建物) trac_content use_type build_type build_ymd area_land 1 土地1建物1車位0 住 公寓(5樓含以下無電梯) 701109 35.0 2 土地1建物1車位0 商 住宅大樓(11層含以上有電梯) 701228 10.7 3 土地1建物1車位1 商 套房(1房1廳1衛) 970114 8.5 4 土地1建物1車位0 商 住宅大樓(11層含以上有電梯) 851218 4.7 5 土地1建物1車位0 住 華廈(10層含以下有電梯) 970624 31.0 area_build area_park price_total price_unit age 1 61 0.0 6380000 105263 32 2 104 0.0 12010000 114928 32 3 52 8.6 10080000 194070 5 4 39 0.0 4600000 116900 17 5 185 0.0 23800000 128510 5
英文欄位名稱 | 中文欄位名稱 |
---|---|
city | 縣市 |
district | 鄉鎮市區 |
trac_year | 交易年份 |
trac_month | 交易月份 |
trac_type | 交易標的 |
trac_content | 交易筆棟數 |
use_type | 使用分區或編定 |
英文欄位名稱 | 中文欄位名稱 |
---|---|
build_type | 建物型態 |
build_ymd | 建築完成年月 |
area_land | 土地移轉總面積.平方公尺. |
area_build | 建物移轉總面積.平方公尺. |
area_park | 車位移轉總面積.平方公尺. |
price_total | 總價.元. |
price_unit | 單價.元.平方公尺. |
str(data)
'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 ...
summary(data)
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
geom_bar
thm <- function() theme(text=element_text(size = 15, family = "STHeiti")) # 控制字體與大小 # STHeiti是只有Mac才有的字體, 用來解決Mac系統中文顯示錯誤的問題 # Windows系統使用者請忽略 `+ thm()` 指令 data %>% ggplot(aes(x=city)) + geom_bar(stat = "count") + thm() # stat = "count" 算個數
data %>% ggplot(aes(x=city)) + geom_bar(stat = "count") + thm() + labs(title="各縣市交易量",x="縣市",y="交易量") # lab用來幫圖形的標題、x軸與y軸做命名
color
vs fill
?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
# 資料整理 data %>% filter(city == "臺北市") %>% group_by(district) %>% summarise(amount=n()) -> table # dplyr::n 用來計數
# A tibble: 12 × 2 district amount <fctr> <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
table %>% ggplot(aes(x=district,y=amount)) + geom_bar(stat = "identity") + thm() # stat='identity'以表格的值做為bar的高度
table %>% ggplot(aes(x=reorder(district,-amount), y=amount)) + geom_bar(stat='identity') + thm() + labs(titles = "臺北市各行政區交易量", x = "行政區", y = "案件數")
use_type
)所佔比例Source: local data frame [10 x 4] Groups: city [2] city use_type amount rate <fctr> <fctr> <int> <dbl> 1 高雄市 工 196 0.006 2 高雄市 農 123 0.004 3 高雄市 其他 532 0.015 4 高雄市 商 7582 0.220 5 高雄市 住 26027 0.755 6 臺北市 工 442 0.018 7 臺北市 農 3 0.000 8 臺北市 其他 1709 0.071 9 臺北市 商 7323 0.302 10 臺北市 住 14761 0.609
data %>% filter(city == "臺北市" | city == "高雄市" ) %>% group_by(city,use_type) %>% summarise(amount = n()) %>% mutate(rate = round(amount/sum(amount),2) ) -> table
stack
table %>% ggplot(aes(x = city, y = rate, fill = use_type)) + geom_bar(stat = 'identity', position = 'stack') + thm() # stack類別堆疊
dodge
table %>% ggplot(aes(x = city, y = rate, fill = use_type)) + geom_bar(stat = 'identity', position = 'dodge') + # dodge類別並排 thm() + scale_fill_discrete(name ="使用分區或編定") # 設定圖例的顯示
geom_line
data %>% group_by(trac_month) %>% summarise(amount=n()) -> table table
# A tibble: 12 × 2 trac_month amount <fctr> <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
table %>% ggplot(aes(x=trac_month,y=amount,group=1)) + geom_line() + thm() + labs(x = "交易月份" , y = "交易數量")
Multiple
Linedata %>% group_by(city,trac_month) %>% summarise(amount=n()) -> table table
Source: local data frame [48 x 3] Groups: city [?] city trac_month amount <fctr> <fctr> <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
Multiple
Linetable %>% ggplot(aes(x=trac_month,y=amount,group=city,color=city)) + geom_line() + geom_point() + thm() + labs(x = "交易月份" , y = "交易數量")
geom_histogram
data %>% ggplot(aes(x=age, y =..count..)) + geom_histogram()
aes(y=..count..)
vs. aes(y=..density..)
data %>% ggplot(aes(x=age, y =..density..)) + geom_histogram()
data %>% ggplot(aes(x=age, y =..density.., fill =..count..)) + # fill 依指定欄位填色 geom_histogram(binwidth=.5)
geom_histogram()
+ geom_density()
data %>% ggplot(aes(x=age,y=..density..)) + geom_histogram(color="black", fill="white") + geom_density(alpha=.2, fill="#FF6666") # alpha設定透明度
geom_boxplot
data %>% ggplot(aes(x=city,y=price_unit)) + geom_boxplot() + thm() + labs(x = "縣市" , y = "每平方公尺價格(元)")
limits
of axes!data %>% ggplot(aes(x=city,y=price_unit)) + geom_boxplot() + thm() + labs(x = "縣市" , y = "每平方公尺價格(元)") + coord_cartesian(ylim = c(0, 2000000)) # 設定y軸上下界線
data %>% ggplot(aes(x=city,y=log(price_unit))) + geom_boxplot() + thm() + # geom_boxplot(outlier.shape = NA) outlier不顯示 labs(x = "縣市" , y = "log(每平方公尺價格(元))")
geom_point
iris %>% ggplot(aes(x=Sepal.Length, y=Petal.Length, color=Species)) + geom_point(shape=1, size=2) # shape控制圖示;size控制點的大小
ggplot2
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 顯示大小
stat
isticsiris %>% ggplot(aes(x = Sepal.Length, y = Petal.Length)) + geom_point() + stat_smooth(method=lm, level=.95) # add se=FALSE to disable CI
facet
data %>% group_by(city,trac_month) %>% # 選擇縣市、交易月份作為分群 summarise(total = n()) -> table # 計算分群下的總數 table %>% ggplot(aes(x = trac_month, y = total ,fill = city))+ geom_bar(stat='identity') + thm()
facet
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()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()
ggsave('檔案名稱') ggsave("plot.pdf", width = 4, height = 4) ggsave("plot.png", width = 4, height = 4, dpi = 300)
Plotly
Python
、R
、Malab
、…等等CRAN
內下載# Plotly is now on CRAN! install.packages("plotly") # install the latest development version (on GitHub) via devtools
或是
從github
上下載,但前提是先
安裝devtools
# install.packages("devtools") devtools::install_github("ropensci/plotly")
library
!library(plotly)
ggplot2
iris %>% ggplot(aes(Petal.Width, Sepal.Length, colour=Species)) + geom_point() -> a a
ggplotly
ggplotly(a)
了解自己的需求,詢問關鍵字與函數
歡迎來信 ling32342@gmail.com 與我一起交流!