美文网首页
R语言基础学习5

R语言基础学习5

作者: 7f0a92cda77c | 来源:发表于2021-06-23 09:25 被阅读0次

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

  1. 基础包
  2. ggplot2 中坚力量
  3. 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-填充颜色
上-基础 下-映射
2.1 绘图-分割界面 -单facet_wrap -双 facet_grid
换个数据集玩 - CO2 data(package = "datasets") 调出R语言自带的所有内置数据集
CO2数据集
ggplot(data = test_1) + 
  geom_point(mapping = aes(x = uptake, y = conc,color = Plant)) +  #aes的参数,是列名
  facet_grid(Type ~ Treatment) #得出的是2*2=4个图的结果,其实是各个列的组合,每个列的种类进行相乘
数据集的每一列的种类
2*2=4张,每个列的种类,只能针对2列,并且是分类数据

映射:分配下,具体分配按照系统设置或者是自己设置下

2.2 几何对象
几何对象可以叠加:局部:全局 上述所生成图片一样
#练习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"
geom_jitter()
4.2 堆叠直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))
fill=clarity
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#需要分组信息


position = "dodge"

看这个更加清爽直接些

5 坐标系
5.1 翻转坐标系
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()#这个代表坐标系翻转
翻转前的-X轴逆时针旋转90°,Y顺时针旋转90° 翻转后的

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()#这个将坐标的方向逆序了
scale_x_reverse()-将X轴的方向进行了逆序变化
5.2 极坐标系coord_polar()
bar <- ggplot(data = diamonds) + 
  geom_bar(
    mapping = aes(x = cut, fill = cut), 
    show.legend = FALSE,
    width = 1#保证是连续的图
  )
width=1
没有width这个参数,是不连续的
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()
bar + coord_polar()
理解这个图:黄-浅绿-深绿-蓝色-黑色,大小是按照这个排得,但是分配得到的圆心角是等同的=360°/5=72°

将坐标-种类按照360°一个圈划分,类似圆形图,纵坐标的值是计数的正值,圆环的半径是纵坐标的计数值。

ggpubr 搜代码直接用,基本不需要系统学习
sthda上有大量ggpubr出的图

相关文章

网友评论

      本文标题:R语言基础学习5

      本文链接:https://www.haomeiwen.com/subject/blqpyltx.html