ggplot绘图是生信技能树生信爆款入门课程R语言部分Day6的讲到的一个重要知识点。为加深理解,现在做下练习巩固。
加载数据
> library(ggplot2)
> test = iris
1.入门级绘图模板
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
2.映射
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))
3.分面
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_wrap(~ Species)
双分面
> 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)
练习
示例数据:ggplot2中数据集mpg
1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
2.尝试修改颜色或大小,从mpg数据框中任选可以用来分类的列。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = drv))
3.根据class列来分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = drv))+
facet_wrap(~ class)
4.根据drv和cyl两个变量来分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = drv))+
facet_wrap(drv~ cyl)
4.几何对象
4.1分组
不分组
> ggplot(data = test) +
+ geom_smooth(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
geom_smooth()
using method = 'loess' and formula 'y ~ x'
根据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'
根据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'
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'
> ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
+ geom_smooth()+
+ geom_point()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
练习
> 1.尝试写出下图的代码
> 数据是iris
> X轴是Species
> y轴是Sepal.Width
> 图是箱线图
>2. 尝试在此图上叠加点图,能发现什么问题?
点数量不够
>3.用下列代码作图,观察结果
> 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'
> 请问,当局部映射和全局映射冲突,以谁为准?
局部
5.统计变换-直方图
> 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))
> ggplot(data = diamonds) +
+ stat_count(mapping = aes(x = cut))
5.1.不统计,数据直接做图
> 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")
5.2count改为prop
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
6.位置关系
6.1抖动的点图
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_point()
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_jitter()
6.2堆叠直方图
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut,fill=clarity))
6.3 并列直方图
> ggplot(data = diamonds) +
+ geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
7.坐标系
翻转coord_flip()
> ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
+ geom_boxplot() +
+ coord_flip()
极坐标系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()
> bar + coord_polar()