ggplot2组合图_组合多个ggplot2图以用于科学出版物

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)
Image for post
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)
Image for post
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 manipulation
library(grid) # combining plots
library(gridExtra) # combining plots
library(ggpubr) # combining plots
library(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)
Image for post

转换为分类变量 (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)
Image for post

准备图 (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
Image for post
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
Image for post
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
Image for post
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
Image for post
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 page
grid.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))
Image for post
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)
Image for post
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 order
grid.arrange(plot3, plot2, plot4, plot1, nrow = 2, ncol = 2)
Image for post
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
Image for post
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
)
Image for post
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
Image for post
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
Image for post
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组合图

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值