作图:ggplot2

拼图 patchwork

导出 eoffice——topptx


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)


绘图函数

基础包

r语言里面 r语言里面e负06是什么意思_开发语言

区分:高级绘图函数/低级绘图函数/绘图参数


plot(iris[,1],iris[,3],col = iris[,5]) text(6.5,4, labels = 'hello') boxplot(iris[,1]~iris[,5]) dev.off()


ggplot2

入门级绘图模板

1、ggplot(data=<DATA>)+<GEOM_FUNCTION>(mapping=aes(<MAPPINGS>))


#data=和mapping=可省略 ggplot(iris)+ geom_point(aes(x=Sepal.Length,y=Petal.Length)) colnames(test)#把数据集的列名列出来后复制粘贴到xy,不容易出错


2、属性设置

r语言里面 r语言里面e负06是什么意思_r语言_02


ggplot(mpg)+geom_point(aes(x=displ,y=hwy),color="blue") #点的大小5mm,透明度50%,点的形状 ggplot(mpg)+geom_point(aes(x=displ,y=hwy),size=5,alpha=0.5,shape=8)


映射:按照数据框的某一列来定义图的某个属性

属于ase()里的参数

是手动设置的延伸

r语言里面 r语言里面e负06是什么意思_图层_03

r语言里面 r语言里面e负06是什么意思_r语言里面_04

1、映射VS手动设置

映射:领导思维,只说按照某列分配颜色,不必须说具体是哪种颜色 [是aes的参数]

手动设置:把所有的点设置为同一个颜色,直接指定是哪个颜色 [不是aes的参数]

2、自行指定映射的具体颜色


ggplot(iris)+geom_point(aes(x=Sepal.Length,y=Petal.Length,color=Species))+ scale_color_manual(values=c("blue","grey","red")) #也有scale_fill_manual(),填充颜色指定


r语言里面 r语言里面e负06是什么意思_直方图_05

3、区分color和fill两个属性

空心形状和实心形状都用color设置颜色; 既有边框又有内心的,才需要color和fill两个参数


#实心例子
ggplot(iris)+geom_point(aes(x=Sepal.Length,y=Petal.Length,color=Species),shape=17)
#空心例子
ggplot(iris)+geom_point(aes(x=Sepal.Length,y=Petal.Length,color=Species),shape=2)
#边框+内心
ggplot(iris)+geom_point(aes(x=Sepal.Length,y=Petal.Length,color=Species),shape=24,fill="black")

#分面
ggplot(iris)+geom_point(aes(x=Sepal.Length,y=Petal.Length))+facet_wrap(~Species)
#双分面
#sample()随机取样函数,replace放回
iris$group=sample(letters[1:5],150,replace=T)
ggplot(iris)+geom_point(aes(x=Sepal.Length,y=Petal.Length))+facet_grid(group~Species)


r语言里面 r语言里面e负06是什么意思_直方图_06

r语言里面 r语言里面e负06是什么意思_开发语言_07

几何对象

1、几何对象可以叠加:局部设置/全局设置


#局部设置:仅对当前图层有效
ggplot(iris)+
  geom_smooth(aes(x=Sepal.Length,y=Petal.Length))+
  geom_point(aes(x=Sepal.Length,y=Petal.Length))
#全部设置:对所有图层有效
ggplot(iris,aes(x=Sepal.Length,y=Petal.Length))+
   geom_smooth()+
   geom_point()


2、当全局设置和局部设置冲突时,以局部设置为准


#拟合线为黑色
ggplot(iris,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
geom_point()+geom_smooth(color = "black")


3、当箱线图和点图联合时,点常因为位置重合在图上无法直观表现【geom_boxplot()+geom_point()】

应用geom_boxplot()+geom_jitter()

r语言里面 r语言里面e负06是什么意思_直方图_08

练习6-2


# 1.尝试写出下图的代码
# 数据是iris
# X轴是Species
# y轴是Sepal.Width
# 图是箱线图,试一试搜一搜,ggplot2箱线图函数是哪个
a=iris
ggplot(data =a)+
  geom_boxplot(mapping = aes(x =Species, y = Sepal.Width))
# 2. 尝试在此图上叠加点图,能发现什么问题?
ggplot(data = test,mapping = aes(x =Species, y = Sepal.Width))+
  geom_boxplot()+
  geom_point()

ggplot(data = test,mapping = aes(x =Species, y = Sepal.Width))+
  geom_point()+
  geom_boxplot()##写在后面的就叠加在上面
 #重复的点都被重叠掩盖了
#解决点发生重叠的办法:
ggplot(data = test,mapping = aes(x =Species, y = Sepal.Width))+
  geom_boxplot()+
  geom_jitter()
 #geom_jitter()牺牲了一点绝对的数值大小,但能让点都分散开
  
# 3.用下列代码作图,观察结果
ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
  geom_point()+
  geom_smooth(color = "black")
ggplot(test,aes(x = Sepal.Length,y = Petal.Length,color = Species)) +
  geom_point()+
  geom_smooth()
  # 请问,当局部设置和全局设置冲突,谁说了算?
     #局部设置
     #ggplot()管全局,geom_()管局部--可以单独改单一图层


5.统计变换

1、直方图


#1 ggplot(diamonds)+geom_bar(aes(x=cut)) #2 ggplot(diamonds)+stat_count(aes(x=cut))


2、使用表中数据直接作图不统计

r语言里面 r语言里面e负06是什么意思_r语言_09

3、不统计count,统计prop(比例)


ggplot(diamonds)+geom_bar(aes(x=cut,y=..prop..,group=1))


对group= 的注释:因为纵轴是..prop..,即分类变量中每个类别占总量的比,group=1就是将这些类别当作一组的这样一个整体去分别计算各个类别的占比,所以须有group=1。
否则,默认的就是各个类别各自一个“组”,在计数时就是普通的条形图,而在计算占比时就成了“各自为政”——每个类别都是百分百占比,所以每个条形图都是顶头的一样高。按照该思想,group=x均可(2、3、4.....)
若是还有填充的映射,如fill=color,则每种颜色代表的color的一个分类在每个条形图中都是高度为1,7种颜色堆叠在一起,纵坐标的顶头都是7。

r语言里面 r语言里面e负06是什么意思_r语言_10

去掉group=1,指定颜色填充

6.位置关系

1)解决点重叠的问题:将geom_point()换成geom_jitter()

r语言里面 r语言里面e负06是什么意思_r语言里面_11


# 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()

# 补充dotplot,点不重合,也不奔放
ggplot(data = mpg,mapping = aes(x = class, 
                                y = hwy,
                                group = class)) + 
  geom_boxplot()+
  geom_dotplot(binaxis = "y",binwidth = .5,stackdir = "center")


r语言里面 r语言里面e负06是什么意思_r语言里面_12

2)堆叠/并列直方图

r语言里面 r语言里面e负06是什么意思_r语言_13

# 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.坐标系

r语言里面 r语言里面e负06是什么意思_图层_14


#翻转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() #右下


完整绘图模板

ggplot(data = <DATA>) +<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>),stat = <STAT>,position = <POSITION>) +<COORDINATE_FUNCTION> +<FACET_FUNCTION>

练习题:


ggplot(iris,aes(x=Sepal.Width,y=Species))+geom_violin(aes(fill=Species))+geom_boxplot()+geom_jitter(aes(shape=Species))


r语言里面 r语言里面e负06是什么意思_开发语言_15

注意点:geom_(功能函数)类比于图层,先写和后写有区别

去除灰色背景代码,theme_set(theme_bw())

ggpubr

ggpubr.R特殊用法:用于组间比较


# 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")
 ##ggplot及ggpubr都能够赋值
##组间两两比较
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
  stat_compare_means(label.y = 9) 
 #label.y = 9 把总的p值放在y=9的位置


r语言里面 r语言里面e负06是什么意思_r语言里面_16

r语言里面 r语言里面e负06是什么意思_r语言里面_17

图片的导出与保存


#图片保存的三种方法

#1.基础包作图的保存
pdf("iris_box_ggpubr.pdf")#保存的格式及文件名
boxplot(iris[,1]~iris[,5])#作图代码
text(6.5,4, labels = 'hello')
dev.off()#画完了,关闭画板

#2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")

#3.eoffice包 导出为ppt神器,全部元素都是可编辑模式
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")


r语言里面 r语言里面e负06是什么意思_直方图_18

dev.new()创建一个新的画板

4、拼图

R包patchwork

  • 语法简单,兼容ggplot2;拼图比例设置简单
  • 支持直接p1+p2拼图,比任何一个包都简单
  • 复杂的布局代码易读性更强
  • 可以给子图添加标记(如ABCD,I II III IV)
  • 可以统一修改所有子图
  • 可以将子图的图例移到一起,整体性好

#修改图例到一边 p1+p2+p3+plot_layout(guides="collect")