R语言做生信ggplot2绘图作图

ggplot2绘图:多张图合并为一张

2019-01-24  本文已影响60人  小明的数据分析笔记本

以下内容来自教程

R语言中多张图画到同一个页面上常用的函数为par()layout()
par()函数详解

layout()函数的简单使用

但是这两个函数不适用于ggplot2ggplot2作图如果希望把多张图放到同一个页面上基本的解决办法是使用(The basic solution is to use the gridExtra R package),主要的两个函数为grid.arrange()arrangeGrob();然而这两个函数都有各自的缺点(说明缺点的英文暂时还没有看懂)

为了解决这个问题,可以使用cowplot这个Rpackage,其中包括一个函数plot_grid();然而这个包也有一个缺点

所以就有了ggpubr包中的ggarrange()函数

第一步安装

两种方式可以选择

第二步使用ggplot2绘图

使用到的数据是ToothGrowth
data("ToothGrowth")
data("mtcars")

library(ggplot2)
df_box<-ToothGrowth
df_box$dose<-factor(df_box$dose)
ggplot(data=df_box,aes(x=dose,y=len,group=dose))+
  geom_boxplot()+theme_bw()
p1
Rplot02.png
df_bar<-mtcars
df_bar$name<-row.names(df_bar)
df_bar$name
p2<-ggplot(data=df_bar,aes(x=reorder(name,mpg), y=mpg))+
  geom_bar(stat="identity")+labs(x="")+theme_bw()+
  theme(axis.text.x=element_text(angle=60,vjust=1,hjust=1))
p2
Rplot08.png
p3<-ggplot(df_bar,aes(x=wt,y=mpg,color=factor(cyl),shape=factor(cyl)))+
  geom_point()+theme_bw()+
  theme(legend.title = element_blank(),
        legend.key.size = unit(2,"cm"),
        legend.background = element_blank(),
        legend.position=c(0.8,0.7))
p3
Rplot09.png

怎么把图例的背景去掉呢???

第三步合并
library(ggpubr)
ggarrange(p1,p2,p3,ncol=2,nrow=2,labels=c("A","B","C"))
Rplot10.png
ggarrange(p2,ggarrange(p1,p3,ncol=2,labels=c("B","C")),nrow=2,labels="A")
Rplot11.png

https://blog.csdn.net/superbfly/article/details/45971791
http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/
https://ggplot2.tidyverse.org/reference/guide_legend.html
https://www.zhihu.com/question/56389259
https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html

上一篇 下一篇

猜你喜欢

热点阅读