ggplot2组合图

The ggplot2 is one of the popular plotting libraries that one could leverage to get beautiful publication-ready plots. The problem arises when you want to combine your multiple plots together.

ggplot2是一种流行的绘图库,可以利用它来获得漂亮的出版物就绪图。 当您要将多个图合并在一起时,就会出现问题。

You may want to combine multiple plots to illustrate comparisons or to display information in an aggregated manner.

您可能需要组合多个图来说明比较或以汇总方式显示信息。

文章大纲 (Article Outline)

  • Aim of this article本文目的
  • Generating Plots生成图
  • Combine plots using grid package使用网格包合并图
  • Combine plots using gridExtra package使用gridExtra软件包合并图
  • Combine plots using ggpubr package使用ggpubr软件包合并图
  • Combine plots using patchwork package使用拼凑包组合地块

目标(Aim)

In this article, we will learn how we could leverage different R’s libraries to combine multiple ggplot2 plots.

在本文中,我们将学习如何利用不同的R库来组合多个ggplot2图。

Observing Dataset

观察数据集

In this article, we are going to use the inbuilt in mtcars dataset. You can see the first 6 observations using the head( ) function.

在本文中,我们将使用内置的mtcars数据集。 您可以使用head()函数查看前6个观察值。

head(mtcars)
A glimpse of the first 6 rows
概览前6行

资料背景(Data Background)

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models) [1].

数据摘自1974年《美国汽车趋势》杂志,其中包括32个汽车(1973–74年型号)的油耗以及汽车设计和性能的10个方面[1]

The data frame contains 32 observations on 11 (numeric) variables.

数据框包含对11个(数字)变量的32个观察值。

  • mpg: Miles/(US) gallon

    mpg :英里/(美国)加仑

  • cyl: Number of cylinders

    cyl :气缸数

  • disp: Displacement (cu. in.)

    disp :排量(立方英寸)

  • hp: Gross Horsepower

    hp :总马力

  • drat: Rear axle ratio

    drat :后轴比率

  • wt: Weight (1000 lbs)

    wt :重量(1000磅)

  • qsec: 1/4 mile time

    qsec :1/4英里时间

  • vs: Engine (0: V-shaped, 1: straight)

    vs :引擎(0:V形,1:直)

  • am: Transmission (0: automatic, 1: manual)

    am :变速箱(0:自动,1:手动)

  • gear: Number of forward gears

    gear :前进档数

  • carb: Number of carburetors

    carb :化油器的数量

mtcars的结构(Structure of mtcars)

Let’s see the data type of each columns using glimpse( ) function from tidyverse package. The tidyverse package contains the base ggplot2 (used for plotting) and dplyr (used for data manipulation) packages.

让我们使用tidyverse包中的glimpse()函数查看每列的数据类型。 tidyverse软件包包含基本的ggplot2(用于绘图)和dplyr(用于数据操作)软件包。

glimpse(mtcars)
data types
资料类型

加载相关库(Loading Relevant Libraries)

The very next step is to load the relevant libraries using library( ) function. You need to install these libraries first using install.packages( ) function.

下一步是使用library()函数加载相关的库。 您需要首先使用install.packages()函数安装这些库。

library(tidyverse) # plotting and manipulationlibrary(grid)      # combining plotslibrary(gridExtra) # combining plotslibrary(ggpubr)    # combining plotslibrary(patchwork) # combining plots

检查类(数据类型) (Checking class (data type))

Here, we are going to use the gear and am variables which are of numeric type. You can check the type using class( ) function.

在这里,我们将使用数字类型的gearam变量。 您可以使用class()函数检查类型。

class(mtcars$gear)class(mtcars$am)

转换为分类变量 (Converting to Categorical Variable)

For plotting, we need to convert both gear and am to categorical variables using as.factor( ) function as both contain few distinct categories.

对于绘图,我们需要使用as.factor()函数将gearam都转换为分类变量,因为两者都包含几个不同的类别。

mtcars$gear <- as.factor(mtcars$gear)mtcars$am <- as.factor(mtcars$am)class(mtcars$gear)class(mtcars$am)

准备图 (Preparing Plots)

To demonstrate how we could combine plots, first, we need to generate a few plots. This plot can be any plot like bar, scatter, box, line etc.

为了演示如何组合图,首先,我们需要生成一些图。 该图可以是任何图,例如条形图,散点图,框形图,线形图等。

Plot1: Scatter plot

图1:散点图

The first plot is a scatter plot where we are going to plot wt on x-axis and mpg on the y-axis and colouring the points based on gear (categorical variable)

第一张图是散点图,我们将在x轴上绘制wt,在y轴上绘制mpg ,并根据齿轮(分类变量)为点着色

plot1 <- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg,                                             colour = gear)) +  geom_point(size=3) +  ggtitle("plot1")plot1
plot1: scatter plot
plot1:散点图

Plot2: Histogram plot

图2:直方图

The second plot is a histogram, where we plot the distribution of mpg variable.

第二个图是直方图,我们在其中绘制mpg变量的分布。

plot2 <- ggplot(data = mtcars, mapping = aes(x = mpg)) +  geom_histogram(binwidth = 5) +  ggtitle("plot2")plot2
plot2: histogram
plot2:直方图

Plot3: Box plot

情节3:箱形图

The third plot is a box plot, wherein the x-axis we plot the interaction of gear and am and in the y-axis, we plot the mpg values.

第三幅图是箱形图,其中x轴绘制出齿轮am的相互作用,y轴绘制出mpg值。

plot3 <- ggplot(data = mtcars,                mapping = aes(x = interaction(gear,am), y=mpg)) +  geom_boxplot() +  ggtitle("plot3")plot3
plot3: boxplot
plot3:箱线图

Plot4: Line plot

曲线4:线图

The fourth (last) plot is a line plot, where we plot hp in the x-axis and mpg in the y-axis.

第四个(最后一个)图是线图,我们在x轴上绘制hp ,在y轴上绘制mpg

plot4 <- ggplot(data = mtcars, mapping = aes(x = hp, y=mpg)) +  geom_line() +  ggtitle("plot4")plot4
plot4: line plot
plot4:线图

使用网格库合并多个图(Combining multiple plots using gird library)

First, we are going to use the grid library to combine the four plots (plot1, plot2, plot3 and plot4).

首先,我们将使用网格库组合四个图(plot1,plot2,plot3和plot4)。

  • First, we need to create an empty page using grid.newpage( ) function

    首先,我们需要使用grid.newpage()函数创建一个空白页

  • Second, we need to use the pushViewport( ) function to push the layout using the viewport( ) function. Here, we have pushed a 2 by 2 layout, means 2 rows and 2 columns for our four plots.

    其次,我们需要使用pushViewport()函数通过viewport()函数来推送布局。 在这里,我们按2 x 2布局,这意味着我们的四个图分别为2行2列。

# Create a new pagegrid.newpage()# Next push the vissible area with a layout of 2 columns and 2 row using pushViewport()pushViewport(viewport(layout = grid.layout(2,2)))

Once you establish the layout, the next step is to send your plots inside the layout using viewport( ) function and inside that, you have to provide the plot object and define its row and column position. Once you push all the defined plots inside your layout it will generate the following combined plot.

建立布局后,下一步是使用viewport()函数将图形发送到布局内,在其中必须提供图对象并定义其行和列位置。 将所有定义的图推送到布局内后,它将生成以下组合图。

# Put the plot on the the area by row and column positionprint(plot1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))print(plot2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))print(plot3, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))print(plot4, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))
Combining plots using grid library
使用网格库合并图

使用gridExtra库合并多个图(Combining multiple plots using gridExtra library)

The next library is gridExtra, which is very simple to use. After loading this library, you need to use the grid.arrange( ) function and inside that, you have to provide the plots one by one, as per the order in which you want to plot it. Further, you need to define the number of rows and columns you want.

下一个库是gridExtra ,使用非常简单。 加载该库后,您需要使用grid.arrange()函数,并且在其中,您必须按照要绘制的顺序逐一提供绘图。 此外,您需要定义所需的行数和列数。

grid.arrange(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2)
Combining plots using gridExtra library
使用gridExtra库合并图

You can change the order of the plots by changing the order of the plot objects inside the grid.arrange( ) function. Here, is an example, where I have changed the order of plot objects (plot3, plot2, plot4, plot1)

您可以通过更改grid.arrange()函数中图对象的顺序来更改图的顺序。 这是一个示例,其中我更改了绘图对象的顺序(plot3,plot2,plot4,plot1)

# Changing the ordergrid.arrange(plot3, plot2, plot4, plot1, nrow = 2, ncol = 2)
Combining plots using gridExtra library
使用gridExtra库合并图

使用ggpubr库合并多个图(Combining multiple plots using ggpubr library)

The next library is one of the popular libraries used for scientific publication, known as ggpubr. The combining mechanism is same as gridExtra library.

下一个图书馆是用于科学出版物的流行图书馆之一,被称为ggpubr。 合并机制与gridExtra库相同。

figure <- ggarrange(plot1, plot2, plot3, plot4,                    ncol = 2, nrow = 2)figure
Combining plots using ggpubr library
使用ggpubr库合并图

You can arrange the plots by nesting the ggarange( ) function in the position of plot objects. Here, I have first plotted object plot4 (extending the entire column width of the first row) and another ggarrange( ) object in second plot position, which comprised of plot1 and plot2.

您可以通过将ggarange()函数嵌套在绘图对象的位置来安排绘图。 在这里,我首先绘制了对象plot4(扩展了第一行的整个列宽),并在第二个绘图位置绘制了另一个ggarrange()对象,该对象由plot1和plot2组成。

ggarrange(  plot4,    # plot4 in first row  ggarrange(plot1, plot2, ncol = 2),   nrow = 2  # plot1 and plot2 in second row)
Reordering plots using ggpubr
使用ggpubr重新排序图

使用拼凑库合并多个图(Combining multiple plots using patchwork library)

The fourth library known as patchwork makes the combining process very simple. After loading the library, you can just use mathematical notations for combining multiple plots. Here, I have combined plot1 and plot2 with just an addition (+) sign.

第四个库称为拼凑而成,使合并过程非常简单。 加载库后,您可以仅使用数学符号来组合多个图。 在这里,我将加上一个加号(+)的plot1和plot2组合在一起。

plot1 + plot2
Combining plots using patchwork library
使用拼凑库组合地块

Let’s say, you want to plot plot1, plot2 and plot3 in row 1 and plot4 in row 2. You can achieve this by providing the plot objects inside a bracket ( ) separated with OR (|) operator. You can use the divide (/) operator to indicate that plot4 should be in next row.

假设您要在第1行中绘制plot1,plot2和plot3,在第2行中绘制plot4。您可以通过在用OR(|)运算符分隔的方括号()中提供绘图对象来实现此目的。 您可以使用除(/)运算符来指示plot4应该在下一行中。

(plot1 | plot2 | plot3) /  plot4
Plots reordering using patchwork library
使用拼凑库对图重新排序

I hope you learned something new. See you next time!

我希望你学到了一些新东西。 下次见!

If you learned something new and liked this article, follow me on onezero.blog (my personal blogging website), Twitter, LinkedIn, YouTube and Github.

如果您学到了新知识并喜欢本文,请在onezero.blog (我的个人博客网站) Twitter LinkedIn YouTube关注 Github

[1] Henderson and Velleman (1981), Building multiple regression models interactively. Biometrics, 37, 391–411.

[1] Henderson和Velleman(1981),以交互方式建立多个回归模型。 生物识别,37,391–411。

Originally published at https://onezero.blog on September 19, 2020.

最初于2020年9月19日https://onezero.blog发布

More Interesting ReadingsI hope you’ve found this article useful! Below are some interesting readings hope you like them too

更多有趣的读物-希望您对本文有所帮助! 以下是一些有趣的读物,希望您也喜欢 -

翻译自: https://towardsdatascience.com/combining-multiple-ggplot2-plots-for-scientific-publications-7dd9908ebe5c

ggplot2组合图


http://www.taodudu.cc/news/show-3880480.html

相关文章:

  • mysql and和or
  • sql 查询使用 and,or和not
  • mysql 多个and or_解决mysql 组合AND和OR带来的问题
  • sql: where条件中AND、OR运算符的组合使用
  • mysql case when多条件同时满足的多个and组合嵌套的情况,判断空is null --- 系列一
  • mysql case when多条件同时满足的多个and组合嵌套的情况,判断空is null --- 系列二
  • Elasticsearch进行and,or多条件组合DSL结构化查询
  • Spring Data Jpa And 和 Or 组合使用的简单方式
  • DBFlow 进阶: or/and组合查询的使用
  • JPA 组合查询之AND和OR组合查询
  • Excel if函数多个条件嵌套与用And/*和Or/+组合条件的使用方法
  • Python pip保姆级环境变量设置
  • Linux环境变量设置(临时+永久)
  • python,pycharm 的环境变量设置
  • git bash环境变量设置
  • cuda环境变量设置
  • Pycharm的环境变量设置
  • SUMO安装及环境变量设置
  • 安装SPARK 环境变量设置
  • windows10 环境变量设置
  • Oracle client环境变量设置
  • centos7环境变量设置
  • oracle环境变量配置教程,oracle_window中oracle环境变量设置方法分享,window server中Oracle的环境变量设 - phpStudy...
  • docker环境变量设置
  • 怎么设置oracle变量环境变量,window中oracle环境变量设置方法分享
  • GCC环境变量设置
  • 设置环境变量的两种方法
  • 水平居中和垂直居中的实现
  • css设置div水平居中方法
  • CSS实现水平居中的四种方法

ggplot2组合图_组合多个ggplot2图以用于科学出版物相关推荐

  1. 关于主机的思维导图_读《思维导图》系列丛书----我用思维导图做什么?

    多图预警! 作者简介:深圳一所公立高中的生物老师,日常是备备课,读读书,打打球,吹吹牛. 我将从以下几个方面介绍我是如何使用思维导图的. 图1:介绍我如何使用思维导图的思维导图 1.我平时用思维导图来 ...

  2. 上证指数30年k线图_上证指数历史k线图_k线图24种经典图解

    上证指数季度k线图 400x296 - 90KB - JPEG 戴若顾比:上证指数寻找新的支撑_股市及时雨 550x341 - 25KB - JPEG 上证综指周K线图 300x232 - 16KB ...

  3. 关于主机的思维导图_关于开展思维导图培训的通知

    关于开展思维导图培训的通知 思维导图(Mind map)是一种有效使用大脑的发散性思考方法,它即是工具和方法.也是一种思维模式,目前全世界有3.5亿人在学习和使用思维导图,被广泛应用在商业.教育.科学 ...

  4. python画美图_学会这些Python美图技巧,就等着女朋友夸你吧

    一.前言 Python中有许多用于图像处理的库,像是Pillow,或者是OpenCV.而很多时候感觉学完了这些图像处理模块没有什么用,其实只是你不知道怎么用罢了.今天就给大家带了一些美图技巧,让你的图 ...

  5. java 鱼刺图_使用java实现鱼刺图

    刚毕业了,这是毕业设计的java web项目中的一个功能,使用鱼刺图进行辅助管理,在做的过程中,没有找到国内有公开的研究资料,不管是哪一种语言,所以就自己做了一个java的鱼刺图生成插件,虽然毕业设计 ...

  6. 上偏续关系哈斯图_上的偏序关系哈斯图如下.PPT

    上的偏序关系哈斯图如下 3. 证明R在A上反对称 任取, ?R??R ? --------.. ? x = y 前提 推理过程 结论 4. 证明R在A上传递 任取,, ?R??R ? -------- ...

  7. mysql的关系图_学会sql数据库关系图

    很久以前就知道微软的Petshop的很经典,昨天抽出时间去学习,一开始还真的不适应,什么成员资格,还真的看不太懂,运行petshop想从登陆学起,但是用户名和密码都不知道,后来发现有更注册的页面,自己 ...

  8. python高级编程第3版导图_流弊!一张图完美解释 Python 3 全部用法

    最近Github热榜中一个Python项目非常抢眼<Python 3 in one pic>. 考虑到有部分同学访问GitHub速度过慢,我特意下载下来,有需要的下方链接自取.GitHub ...

  9. graphpad图片怎么导出矢量图_组合图制作,Graphpad Prism + AI 快、准、稳!

    原标题:组合图制作,Graphpad Prism + AI 快.准.稳! 作者:麦子 转载请注明:解螺旋·临床医生科研成长平台 当我们展示实验结果时,经常需要把有内在逻辑联系的几个图组成一张大图,像这 ...

最新文章

  1. 20个!中国科协发布2020年重大科学问题和工程技术难题
  2. jQuery实现radio第一次点击选中第二次点击取消功能(转)
  3. java学习(92):线程的创建方法一
  4. Android 5.0 最应该实现的8个期望
  5. JSR-303 数据校验学习
  6. [转载] python的next()函数
  7. hdu1558计算几何加并查集
  8. django下的考勤机管理系统定制实例
  9. 每日算法刷题Day3-起始时间转换、二次方根、while连续输入、斐波那契思路
  10. Java、JSP高速公路收费系统
  11. 麦克表单可以做二维码吗_令令开门二维码门禁设备、手机均断网,可以开门吗?...
  12. Centos配置iptables开放ftp服务
  13. 智力题:烧一根不均匀的绳子,从头烧到尾是要1个小时.现在有若干条材质相同的绳子 问如何用烧绳的方法来计时一个小时15分钟.
  14. 基于android系统的单词学习软件设计与开发【附项目源码+论文说明】分享
  15. 向量的平面投影 ProjectOnPlane
  16. ubuntu16.04不能访问新加卷
  17. Android 内存优化- ANR 详解
  18. Codeforces Round #777 (Div. 2) (A-D题解)
  19. zbbz的lisp_Github上四种Lisp方言的流行度
  20. glusterfs java_GlusterFS: 访问权限设置

热门文章

  1. 多种可以反手“调戏”面试官的方案
  2. CPU温度实时预警并将信息发送到钉钉
  3. Eclipse不支持tomcat9解决方法
  4. c语言 strncmp,用C语言实现strncmp函数
  5. 服务端挂了,客户端的TCP连接还会在吗?
  6. nprogress进度条的使用
  7. Vue设置全局css文件以及css组合器
  8. Unity中简单的matcap+fresnel shader的实现
  9. angular2集成手写签名
  10. 添加注册表右键以管理员身份在当前文件夹打开CMD窗口