導(dǎo)入所需包
install.packages("tidyverse")
library(tidyverse)
繪圖模板:
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
【注】ggplot()的第一個(gè)參數(shù)是要在圖中使用的數(shù)據(jù)集,函數(shù) geom_point() 向圖中添加一個(gè)點(diǎn)層,這樣就可以創(chuàng)建一張散點(diǎn)圖。mapping 參數(shù)總是與 aes() 函數(shù)成對出現(xiàn),aes() 函數(shù)的參數(shù)分別指定了映射到 x 軸的變量與映射到 y 軸的變量。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
①將圖中的圖形屬性映射為數(shù)據(jù)集中的變量,可以傳達(dá)出數(shù)據(jù)的相關(guān)信息。例如,可以將點(diǎn)的顏色映射為變量class,從而揭示每輛汽車的類型:
##顏色##
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
###大小size = class;透明度 alpha = class ;形狀 shape = class
②為幾何對象設(shè)置圖形屬性,需將其作為幾何對象函數(shù)的一個(gè)參數(shù),需要在aes( )函數(shù)的外部進(jìn)行設(shè)置。
geom_bar( )條形圖 ;geom_boxplot( ) 箱線圖
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
③facet_wrap( )函數(shù)可通過單個(gè)變量對圖進(jìn)行分面,第一個(gè)參數(shù)是一個(gè)公式,創(chuàng)建公式的方式是在~符號(hào)后面加一個(gè)變量名。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
④通過兩個(gè)變量對圖進(jìn)行分面,需要在繪圖命令中加入facet_grid( )函數(shù),函數(shù)的第一個(gè)參數(shù)也是一個(gè)公式,但該公式包含由 ~ 隔開的兩個(gè)變量名。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
⑤更改幾何對象:曲線
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
⑥按不同線型分類畫出不同的曲線:
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
## 按組分類group = drv; 按顏色分類color = drv
⑦在同一張圖中顯示多個(gè)幾何對象,可以向ggplot()函數(shù)中添加多個(gè)幾何對象函數(shù):
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
? 避免代碼重復(fù),可將一組映射傳遞給ggplot( )函數(shù):
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point( ) +
geom_smooth( )
⑧為不同的圖層指定不同的數(shù)據(jù):
如果將映射放在幾何對象函數(shù)中,那么ggplot2會(huì)將其看作這個(gè)圖層的局部映射,它將使用這些映射擴(kuò)展或覆蓋全局映射,但僅對該圖層有效。這樣一來,我們就可以在不同的圖層中顯示不同的圖形屬性。
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth()
⑨geom_bar( )條形圖:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
### 幾何對象函數(shù)和統(tǒng)計(jì)變換函數(shù)可以互換使用,可以使用stat_count( )替換 geom_bar( ),
###來重新生成前面那張圖。
表示比例(而不是計(jì)數(shù))的條形圖:
ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, y = ..prop.., group = 1)
)
? 檢查連續(xù)變量的分布,可以使用直方圖geom_histogram( ),用 binwidth 參數(shù)來設(shè)定直方圖中間隔的寬度:
ggplot(data = diamonds) +
geom_histogram(mapping = aes(x = carat), binwidth = 0.5)
⑩stat_summary( )函數(shù)為x的每個(gè)唯一值計(jì)算y值的摘要統(tǒng)計(jì):
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)
11)可以使用color= 或者 fill=(這個(gè)更有用)圖形屬性來為條形圖上色:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, color = cut))
###如果將fill圖形屬性映射到另一個(gè)變量(如 clarity),那么條形會(huì)自動(dòng)分塊堆疊起來,
###每個(gè)彩色矩形表示 cut 和 clarity 的一種組合。
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity))
12)避免散點(diǎn)圖重疊,利用position = "jitter"可以為每個(gè)數(shù)據(jù)點(diǎn)添加一個(gè)很小的隨機(jī)擾動(dòng),這樣就可以將重疊的點(diǎn)分散開來:
ggplot(data = mpg) +
geom_point(
mapping = aes(x = displ, y = hwy),
position = "jitter"
)
13)coord_flip( )函數(shù)可以交換x軸和y軸:
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
####
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot( ) +
coord_flip( )
14)geom_abline( )函數(shù)
abline(a,b) 表示畫一條y=ax+b的直線
abline(y) 表示畫一條過所有點(diǎn)的水平直線
abline(v=x) 表示畫一條過所有點(diǎn)的豎直直線
abline(lm,obj) 表示畫出線性模型得到的線性方程
**向前面的代碼模板中添加位置調(diào)整、統(tǒng)計(jì)變換、坐標(biāo)系和分面:
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>