ggplot繪圖是生信技能樹(shù)生信爆款入門課程R語(yǔ)言部分Day6的講到的一個(gè)重要知識(shí)點(diǎn)。為加深理解,現(xiàn)在做下練習(xí)鞏固。
加載數(shù)據(jù)
> library(ggplot2)
> test = iris
1.入門級(jí)繪圖模板
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
image.png
2.映射
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))
image.png
3.分面
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_wrap(~ Species)
image.png
雙分面
> test$Group = sample(letters[1:5],150,replace = T)
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_grid(Group ~ Species)
image.png
練習(xí)
示例數(shù)據(jù):ggplot2中數(shù)據(jù)集mpg
1.分別以mpg的displ和hwy兩列作為橫縱坐標(biāo),畫點(diǎn)圖恬叹。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
image.png
2.嘗試修改顏色或大小,從mpg數(shù)據(jù)框中任選可以用來(lái)分類的列。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = drv))
image.png
3.根據(jù)class列來(lái)分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = drv))+
facet_wrap(~ class)
image.png
4.根據(jù)drv和cyl兩個(gè)變量來(lái)分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = drv))+
facet_wrap(drv~ cyl)
image.png
4.幾何對(duì)象
4.1分組
不分組
> ggplot(data = test) +
+ geom_smooth(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
image.png
geom_smooth()
using method = 'loess' and formula 'y ~ x'
根據(jù)Species分組
> ggplot(data = test) +
+ geom_smooth(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ group = Species))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
image.png
根據(jù)Species著色
> ggplot(data = test) +
+ geom_smooth(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
image.png
4.2圖層
局部映射和全局映射
> ggplot(data = test) +
+ geom_smooth(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
image.png
> ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
+ geom_smooth()+
+ geom_point()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
image.png
練習(xí)
> 1.嘗試寫出下圖的代碼
> 數(shù)據(jù)是iris
> X軸是Species
> y軸是Sepal.Width
> 圖是箱線圖
image.png
>2. 嘗試在此圖上疊加點(diǎn)圖执赡,能發(fā)現(xiàn)什么問(wèn)題?
點(diǎn)數(shù)量不夠
>3.用下列代碼作圖函筋,觀察結(jié)果
> ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
+ geom_point()+
+ geom_smooth(color = "black")
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
> 請(qǐng)問(wèn)沙合,當(dāng)局部映射和全局映射沖突,以誰(shuí)為準(zhǔn)跌帐?
局部
5.統(tǒng)計(jì)變換-直方圖
> View(diamonds)
> table(diamonds$cut)
Fair Good Very Good Premium Ideal
1610 4906 12082 13791 21551
>
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut))
image.png
> ggplot(data = diamonds) +
+ stat_count(mapping = aes(x = cut))
image.png
5.1.不統(tǒng)計(jì)首懈,數(shù)據(jù)直接做圖
> fre = as.data.frame(table(diamonds$cut))
> fre
Var1 Freq
1 Fair 1610
2 Good 4906
3 Very Good 12082
4 Premium 13791
5 Ideal 21551
>
> ggplot(data = fre) +
+ geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
image.png
5.2count改為prop
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
image.png
6.位置關(guān)系
6.1抖動(dòng)的點(diǎn)圖
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_point()
image.png
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_jitter()
image.png
6.2堆疊直方圖
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut,fill=clarity))
image.png
6.3 并列直方圖
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
image.png
7.坐標(biāo)系
翻轉(zhuǎn)coord_flip()
> ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
+ geom_boxplot() +
+ coord_flip()
image.png
極坐標(biāo)系coord_polar()
bar <- ggplot(data = diamonds) +
+ geom_bar(
+ mapping = aes(x = cut, fill = cut),
+ show.legend = FALSE,
+ width = 1
+ ) +
+ theme(aspect.ratio = 1) +
+ labs(x = NULL, y = NULL)
> bar + coord_flip()
image.png
> bar + coord_polar()
image.png