1 需要安装的包这些
if(!require(ggplot2))install.packages('ggplot2',update = F,ask = F)
if(!require(ggpubr))install.packages('ggpubr',update = F,ask = F)
if(!require(eoffice))install.packages("eoffice",update = F,ask = F)
if(!require(patchwork))install.packages("patchwork",update = F,ask = F)
STHDA美图中心:www.sthda.com
2 目前作图分3类,拼图常用patchwork
- 基础包
- ggplot2 中坚力量
- ggpubr
图就是数据,数据就是图,ggplot2
优先会
2.1 绘图必会的-作图数据、纵横坐标
#数据集是iris
test = iris
#入门级绘图模板
ggplot(data = <DATA>)+
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
#映射-将某一数据作为分类数据,在图上得到显示
ggplot(data = test)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
#除此之外,有别的参数-color, size-大小, shape-形状, alpha-透明度,fill-填充颜色
![](https://img.haomeiwen.com/i13524692/1d6cf514ea4e0fa9.png)
2.1 绘图-分割界面 -单facet_wrap
-双 facet_grid
换个数据集玩 - CO2 data(package = "datasets")
调出R语言自带的所有内置数据集
![](https://img.haomeiwen.com/i13524692/dec6146d5aa42523.png)
ggplot(data = test_1) +
geom_point(mapping = aes(x = uptake, y = conc,color = Plant)) + #aes的参数,是列名
facet_grid(Type ~ Treatment) #得出的是2*2=4个图的结果,其实是各个列的组合,每个列的种类进行相乘
![](https://img.haomeiwen.com/i13524692/3fd665c45c631dce.png)
![](https://img.haomeiwen.com/i13524692/28b20f29c9a9544f.png)
映射:分配下,具体分配按照系统设置或者是自己设置下
2.2 几何对象
![](https://img.haomeiwen.com/i13524692/cbf30b950b2f5af0.png)
![](https://img.haomeiwen.com/i13524692/36db05800348ae8f.png)
#练习6-2
# 1.尝试写出下图的代码
# 数据是iris
# X轴是Species
# y轴是Sepal.Width
# 图是箱线图
ggplot(data = test) +
geom_boxplot(mapping = aes(x = Species, y = Sepal.Width))
当局部映射和全局映射冲突,局部变量为准
3 统计变换-直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.. , group = 1))
4 位置关系
4.1 抖动的点图 - 散点加上去geom_jitter()
ggplot(data = mpg,mapping = aes(x = class,
y = hwy,
group = class)) +
geom_boxplot()+
geom_jitter()# 默认positon = "stack"
![](https://img.haomeiwen.com/i13524692/2ef1a1816f3e5d69.png)
4.2 堆叠直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
![](https://img.haomeiwen.com/i13524692/09cb00ebb36f94a6.png)
4.3 并列直方图-在上面的基础上加了一个参数-position="dodge"
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
Dodging preserves the vertical position of an geom while adjusting the horizontal position. #在在调整水平位置的同时保留几何的垂直位置。position_dodge () requires the grouping variable to be be specified in the global or geom_* layer#需要分组信息
![](https://img.haomeiwen.com/i13524692/299b2ef3d9ec4e24.png)
看这个更加清爽直接些
5 坐标系
5.1 翻转坐标系
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()#这个代表坐标系翻转
![](https://img.haomeiwen.com/i13524692/cb49d984cb8bac0a.png)
![](https://img.haomeiwen.com/i13524692/5898372abd1ae0eb.png)
coord_flip()# geoms in the horizontal instead of vertical position.
h <- ggplot(diamonds, aes(carat)) +
geom_histogram()
h
h + coord_flip()
h + coord_flip() + scale_x_reverse()#这个将坐标的方向逆序了
![](https://img.haomeiwen.com/i13524692/f50f6473e3541022.png)
5.2 极坐标系coord_polar()
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1#保证是连续的图
)
![](https://img.haomeiwen.com/i13524692/26624b1fdaa2c203.png)
![](https://img.haomeiwen.com/i13524692/5efed1ce2dd50927.png)
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_polar()
![](https://img.haomeiwen.com/i13524692/ced2f96183b4d174.png)
理解这个图:黄-浅绿-深绿-蓝色-黑色,大小是按照这个排得,但是分配得到的圆心角是等同的=360°/5=72°
将坐标-种类按照360°一个圈划分,类似圆形图,纵坐标的值是计数的正值,圆环的半径是纵坐标的计数值。
网友评论