如何在R中使用ggplot2并排绘制两个图形?

时间:2019-07-16 12:29:47

标签: r pdf graph gridextra par

我正在尝试在R中使用gplot2并排绘制2个图。我希望​​它们既可以png也可以pdf文件打印。以下是我的测试数据框,称为“ data_select”:

Biomarkers  Age Sex Percentile  Percentiles
GeneX   42.5    men 25  0.028
GeneX   42.5    women   25  0.022
GeneX   47.5    men 25  0.027
GeneX   47.5    women   25  0.023
GeneX   52.5    men 25  0.027
GeneX   52.5    women   25  0.024
GeneX   57.5    men 25  0.029
GeneX   57.5    women   25  0.023
GeneX   62.5    men 25  0.03
GeneX   62.5    women   25  0.024
GeneX   67.5    men 25  0.033
GeneX   67.5    women   25  0.026
GeneX   72.5    men 25  0.028
GeneX   72.5    women   25  0.022
GeneX   42.5    men 50  0.036
GeneX   42.5    women   50  0.03
GeneX   47.5    men 50  0.036
GeneX   47.5    women   50  0.029
GeneX   52.5    men 50  0.035
GeneX   52.5    women   50  0.029
GeneX   57.5    men 50  0.039
GeneX   57.5    women   50  0.031
GeneX   62.5    men 50  0.038
GeneX   62.5    women   50  0.03
GeneX   67.5    men 50  0.04
GeneX   67.5    women   50  0.035
GeneX   72.5    men 50  0.033
GeneX   72.5    women   50  0.032
GeneX   42.5    men 75  0.049
GeneX   42.5    women   75  0.04
GeneX   47.5    men 75  0.049
GeneX   47.5    women   75  0.036
GeneX   52.5    men 75  0.045
GeneX   52.5    women   75  0.038
GeneX   57.5    men 75  0.051
GeneX   57.5    women   75  0.041
GeneX   62.5    men 75  0.047
GeneX   62.5    women   75  0.04
GeneX   67.5    men 75  0.05
GeneX   67.5    women   75  0.043
GeneX   72.5    men 75  0.049
GeneX   72.5    women   75  0.04

这是我想并排打印的两个图形(男人和女人)的代码。

# Load libraries
library(ggplot2)
library(magrittr)
library(dplyr)

# Select for men and women
data_select_men <- data_select[ data_select$Sex %in% "men", ]
data_select_women <- data_select[ data_select$Sex %in% "women", ]

pdf ("graph.pdf")
par(mfrow=c(1,2))

# Graph for men
check_men <- data_select_men %>%
    ggplot(aes(x = data_select_men$Age,
        y = data_select_men$Percentiles,
        group = data_select_men$Percentile))
check_men <- check_men +
    geom_line(aes(color = data_select_men$Percentile), na.rm = TRUE) +
    geom_point(aes(color = data_select_men$Percentile),
                size = 1,
                na.rm = TRUE) +
    # labs(x = NULL) + 
    labs(title="GeneX (men)", x="Age groups", y="Percentiles") + 

    scale_x_continuous(breaks = c(42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5),
        labels = c("40-45", "45-50", "50-55", "55-60", "60-65", "65-70", "70-75")) +
    scale_y_continuous(breaks = c(0.025, 0.050, 0.075, 0.100),
        labels = c("25", "50", "75", "100"))

# Graph for women
check_women <- data_select_women %>%
    ggplot(aes(x = data_select_women$Age,
        y = data_select_women$Percentiles,
        group = data_select_women$Percentile))
check_women <- check_women +
    geom_line(aes(color = data_select_women$Percentile), na.rm = TRUE) +
    geom_point(aes(color = data_select_women$Percentile),
                size = 1,
                na.rm = TRUE) +
    # labs(x = NULL) + 
    labs(title="GeneX (women)", x="Age groups", y="Percentiles") + 

    scale_x_continuous(breaks = c(42.5, 47.5, 52.5, 57.5, 62.5, 67.5, 72.5),
        labels = c("40-45", "45-50", "50-55", "55-60", "60-65", "65-70", "70-75")) +
    scale_y_continuous(breaks = c(0.025, 0.050, 0.075, 0.100),
        labels = c("25", "50", "75", "100"))

dev.off()

目前,我从中得到的是一个pdf文件,共2页(每页1张图)。我想要的是一个pdf文件,一页,而这2张图形并排。

您已经看到,我已经使用了“ par(mfrow = c(1,2))”,但是它不起作用。此外,我还尝试在“ dev.off”之前的以下几行中使用“ gridExtra”软件包。

library(gridExtra)
grid.arrange(check_men, check_women, nrow = 1) # I also tried using ncol = 2.

但这也不起作用。它给了我一个3页的pdf文件。前2页包含正确的图,最后一页包含2个图,但是这2个图为空白并且不包含所有属性。

任何人都可以帮助我,让我了解并排准确的图表吗?谢谢。

0 个答案:

没有答案