06 R语言作图
图就是数据,数据就是图
常用可视化R包
作图:base,ggplot2, ggpubr;
拼图:patchwork;
导出:pdf()等三段论, ggsave, eoffice-topptx。
![](https://img.haomeiwen.com/i25993941/c15a123bb7b70350.png)
低级绘图参数作用是在高级绘图函数添砖加瓦,而绘图参数存在于函数内部,在没有设定值时使用默认值。
> plot(iris[,1],iris[,3],col = iris[,5]) #plot是高级绘图函数
> text(6.5,4, labels = 'hello')#text是低级绘图函数,加标注的
#label为绘图参数
plot最古老最丑
ggplot背景为灰色,图注在右侧
ggpubr简化/美化,图注在上方
ggplot2语法
1.入门级绘图模版:作图数据,横纵坐标
> library(ggplot2)
> test = iris
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length))
2.手动设置与映射
2.1 手动设置,需要设置为有意义的值
颜色color 大小 size 形状shape 透明度 alpha 填充颜色fill
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy),
+ color = "blue")#color是geompoint的参数,是具体颜色
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy),
+ size = 5, # 点的大小5mm
+ alpha = 0.5, # 透明度 50%
+ shape = 8) # 点的形状可以为数字或手动敲出来的形状
> #手动设置只能设置出1种颜色
2.2 映射:按照数据框的某一列来定义图的某个属性
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))#color是aes的参数,是列名
Q1 能不能自行指定映射的具体颜色?
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species))+
+ scale_color_manual(values = c("blue","grey","red"))#指定映射的具体颜色
Q2 区分color和fill两个属性
Q2-1 空心形状和实心形状都用color设置颜色
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 17) #17号,实心的例子
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 2) #2号,空心的例子
Q2-2 既有边框又有内心的,才需要color(边框)和fill(内心)两个参数
> ggplot(data = test)+
+ geom_point(mapping = aes(x = Sepal.Length,
+ y = Petal.Length,
+ color = Species),
+ shape = 24,
+ fill = "black") #22号,双色的例子
3.分面
单分面
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_wrap(~ Species) #按Species进行分面
双分面
> test$Group = sample(letters[1:5],150,replace = T)#新增1列
> ggplot(data = test) +
+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) +
+ facet_grid(Group ~ Species)#按Group分行,按Species分列
#练习6-1
> # 示例数据:ggplot2中的数据集mpg
> # 1.分别以mpg的displ和hwy两列作为横纵坐标,画点图。
> library(ggplot2)
> test=mpg
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy))
> # 2.尝试修改颜色或大小,从mpg数据框中任选可以用来分类的列。
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ,
+ y = hwy,
+ color=class))
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ,
+ y = hwy,
+ color=displ),size=1)
> # 3.根据class列来分面
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ, y = hwy,color=displ),size=1)+facet_wrap(~class)
> # 4.根据drv和cyl两个变量来分面
> ggplot(data = mpg) +
+ geom_point(mapping = aes(x = displ,
+ y = hwy,
+ color=displ),size=1)+
+ facet_grid(drv~cyl)
4.几何对象(约等于图层)
![](https://img.haomeiwen.com/i25993941/5e4a582c2a2d5f81.png)
![](https://img.haomeiwen.com/i25993941/e87b8edeae660dec.png)
> 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'
5.统计变换
#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.2 count改为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()
> ggplot(data = mpg,mapping = aes(x = class,
+ y = hwy,
+ group = class)) +
+ geom_boxplot()+
+ geom_dotplot(binaxis = "y",binwidth = .5,stackdir = "center")
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()
网友评论