我们通常会希望使用ggplot2创建横向、纵向并列图,方便同时对比查看数据的不同方面特性。我们可以使用patchwork包很容易实现。

加载包


#install ggplot2 and patchwork packages
install.packages('ggplot2')
install.packages('patchwork')#load the packages
library(ggplot2)
library(patchwork)

本文我们展示几个示例教你使用这些包创建多个并列图。

两个并列图

下面代码展示如何创建两个并列图,使用R内置的iris数据集:

#create box plot
plot1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +geom_boxplot()#create density plot
plot2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +geom_density(alpha = 0.8)#display plots side by side
plot1 + plot2

三个并列图

下面代码展示如何创建三个并列图:

#create box plot
plot1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +geom_boxplot()#create density plot
plot2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +geom_density(alpha = 0.7)#create scatterplot
plot3 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +geom_point(aes(colour=factor(Species), shape=factor(Species)))#display three plots side by side
plot1 + plot2 + plot3

两个垂直排列图

下面代码展示如何创建两个垂直排列图:

#create box plot
plot1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +geom_boxplot()#create density plot
plot2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +geom_density(alpha = 0.7)#display plots stacked on top of each other
plot1 / plot2

组合排列

下面展示上面一行一个图,下面一行并列展示两个图:

#create box plot
plot1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +geom_boxplot()#create density plot
plot2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +geom_density(alpha = 0.7)#create scatterplot
plot3 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +geom_point(aes(colour=factor(Species), shape=factor(Species)))#display plots stacked on top of each other
plot1 / (plot2 + plot3)

给组合图增加标题、子标题、题注

plot_annotation函数可以实现对组合图添加标题、子标题、题注等,下面代码展示如何实现:

#create box plot
plot1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +geom_boxplot() +ggtitle('Boxplot')#create density plot
plot2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +geom_density(alpha = 0.7) +ggtitle('Density Plot')#display plots side by side with title, subtitle, and captions
patchwork <- plot1 + plot2 patchwork + plot_annotation(title = 'This is a title',subtitle = 'This is a subtitle that describes more information about the plots',caption = 'This is a caption',theme = theme(plot.title = element_text(size = 19))
)

ggplot2画组合图相关推荐

  1. 如何用python画组合图形_python_matplotlib画组合图

    下图是要画的组合图的样式,用到的核心方法是plt.axes方法.方法内的参数是一个list,由左下角坐标,宽,高组成:[x,y,w,h](图中打勾处),注意在构建list的时候考虑进去图与图的间隙,图 ...

  2. 热图ggplot pheatmap ggplot2画热图及个性化修饰

    免疫浸润计算方法是CIBERSORT和ssgsea 画图_YoungLeelight的博客-CSDN博客 ggplot画热图 合并细胞组合细胞 单细胞基因整体表达量 合并多个细胞整体表达量条形热图 合 ...

  3. R语言学习:用ggplot2画折线图

    本文是个人学习笔记 想把图画做的好看一点,于是学习了一下R语言里的ggplot包. 一.数据集 因为我需要画三个组的平均数据,首先要对每个组的被试数据进行平均,然后这样摆放数据: group bloc ...

  4. R软件-ggplot2 画火山图

    R软件 ggplot2 1 导入包 # 帮助文档链接:http://docs.ggplot2.org/current/ library(ggplot2) 2.改变工作路径,将工作路径改变到数据存放的文 ...

  5. 以题促学01_matplotlib画图_分别用subplots和subplot,add_subplot3种方法画组合图

    题目要求: 要求 1.一个画布上,有四个子图,画布大小设置为 6,6 2.左上子图,折线图,y=x*x 3.右上子图,散点图,x轴范围(0-10)之间整数,y轴为(0-1)之间正太分布数据 4.左下子 ...

  6. Pyecharts 折线图与堆积柱状图结合的组合图绘画,折线图数据点在柱状图柱中心

    问题/背景 最近因科研需要,开始浅浅学习pyecharts,并记录在这个过程遇到的问题以及简单的解决办法. 在使用pyecharts画组合图时遇到了折线图的点无法对准柱状图中心的问题,在网上的文章只找 ...

  7. 不需要懂得编程,但却可以使用ggplot2画出论文级别的图?

    你有没有遇到过这样的烦恼,你需要画一些论文级别的图,并且你知道R中的ggplot2是一个很好的选择,可以画出符合你要求的图.但是由于你不熟悉ggplot2的使用,你需要上网倒弄一番,了解与你图相关的代 ...

  8. R语言ggplot2可视化:使用patchwork包绘制ggplot2可视化结果的组合图(自定义图像的嵌入关系)、使用patchwork包绘制ggplot2可视化结果的组合图(自定义组合形式)

    R语言ggplot2可视化:使用patchwork包绘制ggplot2可视化结果的组合图(自定义图像的嵌入关系).使用patchwork包绘制ggplot2可视化结果的组合图(自定义组合形式) 目录

  9. R语言ggplot2可视化为组合图添加综合图例实战:使用ggpubr包ggarrange函数实现综合图例、使用patchwork包实现综合图例

    R语言ggplot2可视化为组合图添加综合图例实战:使用ggpubr包ggarrange函数实现综合图例.使用patchwork包实现综合图例 目录

最新文章

  1. Mysql —— C语言链接mysql数据库,实现可以增删改查的角色权限登录系统
  2. ddr test DCD CFG file CBT
  3. 0207.Domino R8.0.x群集配置手册
  4. powershell 脚本运行策略,参数....
  5. [MySQL FAQ]系列 -- 数据不算大,备份却非常慢
  6. UESTC 250 windy数 数位dp
  7. 计算机基础优秀教案范文,《计算机基础知识与基本操作》教学课例(教学设计三等奖)...
  8. (73)FPGA面试题-Verilog实现5人表决器
  9. 工程力学考研 可以转计算机专业吗,跨专业考研我是工程力学的本科生,想要考飞行 – 手机爱问...
  10. 传智播客javascript视频教程(杨中科)学习笔记
  11. dae怎么用草图大师打开,教你su模型导入lumion的方法
  12. Mac outlook设置自动回复
  13. Debugger and device times had drifted by more than 60s. Please correct this by running adb shell da
  14. SQLite使用模糊查询
  15. Python开发mysql和mongo 连接类
  16. H3C恢复console登录密码
  17. c语言求圆锥的表面积和体积_c语言如何编程求圆体积和表面积
  18. php float 取整,php float 转 int 的问题
  19. javaScript 美化radio
  20. 拦截手机发送的请求,对请求信息进行获取

热门文章

  1. 理解神经网络的注意力机制(Attention)及PyTorch 实现
  2. [转]ASP.NET MVC3 + EF 性能优化解决方案以及最优架构
  3. c语言程序设计智慧树百度文库,C语言程序设计(原版)
  4. 如何解决Keil报错error: #411: a parameter is not allowed ...
  5. 强人工智能不能碰,那电影中的机器人只是一场梦?
  6. 平面设计软件都有哪些?推荐这7款
  7. bzoj3007: 拯救小云公主(二分+并查集)
  8. 1426:【例题5】智力大冲浪
  9. xamarin android 报错 Could not load assembly 'Xamarin.Android.Support.v7.AppCompat
  10. [桌面运维] 显示器 色准,色域,色深,分辨率,带宽,刷新率的基本概念,图像呈现的基本原理