美文网首页
五、R 语言作图

五、R 语言作图

作者: 白米饭睡不醒 | 来源:发表于2021-01-18 18:24 被阅读0次
1

1.绘图函数

(高级函数能绘成一张图,低级函数是添砖加瓦的)

2

(1)绘图参数

3

(2)手动参数

4

(3)模板

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))
#(映射,根据数据中的列分配颜色)
ggplot(data = test)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length),
                           color = "blue")
#(把所有的点手动变成蓝色)

#3.分面
ggplot(data = test) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
#(facet_wrap(~ Species) 意思是按照Species这一列分为三张子图)
#(facet_wrap(~ Species) 与前命令之间必须加号连接)
#(可用nrow=     ncol=    调整行列

#双分面
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) 
#(按两列分面)

2.几何对象

(1)分组

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))


ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                            y = Petal.Length,
                            group = Species))
#group显式的分组

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length,
                          color = Species)) 
#隐式的分组

(2)图层

几何对象可以叠加

ggplot(data = test) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length))#局部映射

ggplot(data = test,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+
  geom_point()
#全局映射,接下来的横纵坐标都是同一个
#如果前后都设置了颜色或别的以后面为最后结果,未设置的随全局图层变化

5

3.统计变换

#直方图
View(diamonds)
table(diamonds$cut)

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))#画柱状图

ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))#统计数量的最好的图
  • 使用场景
# 1)不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre

ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
#stat = "identity"意思不要统计,已给出横纵坐标直接做图就好了

# 2)count改为prop
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
#不统计数量,只显示所占比例

4.位置关系

(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()
#geom_jitter()可将纵坐标范围变大,让点不再重合

(2)堆叠直方图

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))
#可看到每一个颜色在分组的比例

(3)并列直方图

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), 
                     position = "dodge")
#可看出具体谁大谁小

(4)坐标系

#翻转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()
  • 完整绘图模板 (theme可更改ggplot主题背景颜色)
6

5.ggpubr

# ggpubr 搜代码直接用,基本不需要系统学习

# sthda上有大量ggpubr出的图
library(ggpubr)
ggscatter(iris,x="Sepal.Length",
          y="Petal.Length",
          color="Species")

p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
p
my_comparisons <- list( c("setosa", "versicolor"), 
                        c("setosa", "virginica"), 
                        c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons)+ 
# Add pairwise comparisons p-value组间比较,可用+与ggplot连用
  stat_compare_means(label.y = 9) 

6.图片保存

(探索保存为PPT?)

ggsave("iris_box_ggpubr.png",width = 10,height = 7)
#""后逗号加tab键可调整保存图片的大小等
ggsave(p,filename ="iris_box_ggpubr.png" )
#p代表的图保存在"iris_box_ggpubr.png"文件里
#在画板上没有东西的情况下才能运行成功

7.拼图(patchwork)

library(patchwork)
p1+p2+p3#p1..p3是把图命名
6

相关文章

网友评论

      本文标题:五、R 语言作图

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