如何使用 ggplot2 和 ggpubr 编辑通用图例标题名称?

How can I edit the common legend title name using ggplot2 and ggpubr?

我正在使用 ggpubr 将多个图形合并到一个图中,但似乎无法正确生成一个带有我想要的标题的图形。我希望标题是“定制图例”,因为它是两个图表的共同图例。有人知道我该怎么做吗?

这是我的数据:

data1 = data.frame(var1 = c(1,
                            1,
                            1,
                            1,
                            2,
                            2,
                            2,
                            2,
                            3,
                            3,
                            3,
                            3,
                            4,
                            4,
                            4,
                            4,
                            5,
                            5,
                            5,
                            5,
                            6,
                            6,
                            6,
                            6,
                            7,
                            7,
                            7,
                            7,
                            8,
                            8,
                            8,
                            8,
                            9,
                            9,
                            9,
                            9,
                            10,
                            10,
                            10,
                            10,
                            11,
                            11,
                            11,
                            11,
                            12,
                            12,
                            12,
                            12,
                            13,
                            13,
                            13,
                            13,
                            14,
                            14,
                            14,
                            14,
                            15,
                            15,
                            15,
                            15,
                            16,
                            16,
                            16,
                            16,
                            17,
                            17,
                            17,
                            17,
                            18,
                            18,
                            18,
                            18,
                            19,
                            19,
                            19,
                            19,
                            20,
                            20,
                            20,
                            20,
                            21,
                            21,
                            21,
                            21,
                            22,
                            22,
                            22,
                            22,
                            23,
                            23,
                            23,
                            23,
                            24,
                            24,
                            24,
                            24,
                            25,
                            25,
                            25,
                            25,
                            26,
                            26,
                            26,
                            26,
                            27,
                            27,
                            27,
                            27,
                            28,
                            28,
                            28,
                            28,
                            29,
                            29,
                            29,
                            29,
                            30,
                            30,
                            30,
                            30,
                            31,
                            31,
                            31,
                            31,
                            32,
                            32,
                            32,
                            32,
                            33,
                            33,
                            33,
                            33),
                   var2 = c(1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4,
                            1,
                            2,
                            3,
                            4),
                   var3 = c(113,
                            89,
                            99,
                            41,
                            72,
                            64,
                            39,
                            139,
                            135,
                            17,
                            3,
                            135,
                            63,
                            126,
                            34,
                            87,
                            84,
                            125,
                            123,
                            18,
                            115,
                            11,
                            68,
                            85,
                            48,
                            95,
                            56,
                            129,
                            41,
                            78,
                            82,
                            122,
                            124,
                            4,
                            60,
                            132,
                            67,
                            128,
                            46,
                            79,
                            110,
                            88,
                            19,
                            88,
                            88,
                            126,
                            30,
                            11,
                            52,
                            66,
                            15,
                            52,
                            6,
                            74,
                            14,
                            101,
                            88,
                            70,
                            58,
                            20,
                            104,
                            76,
                            134,
                            23,
                            40,
                            1,
                            47,
                            25,
                            49,
                            110,
                            96,
                            100,
                            106,
                            26,
                            93,
                            19,
                            87,
                            41,
                            13,
                            40,
                            63,
                            87,
                            137,
                            105,
                            89,
                            95,
                            24,
                            49,
                            112,
                            92,
                            45,
                            105,
                            112,
                            105,
                            114,
                            129,
                            84,
                            33,
                            95,
                            95,
                            15,
                            90,
                            1,
                            62,
                            20,
                            7,
                            18,
                            96,
                            4,
                            71,
                            42,
                            94,
                            45,
                            102,
                            55,
                            98,
                            124,
                            80,
                            76,
                            97,
                            41,
                            31,
                            25,
                            21,
                            135,
                            138,
                            121,
                            93,
                            17,
                            13,
                            49,
                            26))



data2 <- data.frame(var1a = c(1,
                              1,
                              1,
                              1,
                              2,
                              2,
                              2,
                              2,
                              3,
                              3,
                              3,
                              3,
                              4,
                              4,
                              4,
                              4,
                              5,
                              5,
                              5,
                              5,
                              6,
                              6,
                              6,
                              6,
                              7,
                              7,
                              7,
                              7,
                              8,
                              8,
                              8,
                              8,
                              9,
                              9,
                              9,
                              9,
                              10,
                              10,
                              10,
                              10,
                              11,
                              11,
                              11,
                              11,
                              12,
                              12,
                              12,
                              12,
                              13,
                              13,
                              13,
                              13,
                              14,
                              14,
                              14,
                              14,
                              15,
                              15,
                              15,
                              15,
                              16,
                              16,
                              16,
                              16,
                              17,
                              17,
                              17,
                              17,
                              18,
                              18,
                              18,
                              18,
                              19,
                              19,
                              19,
                              19,
                              20,
                              20,
                              20,
                              20,
                              21,
                              21,
                              21,
                              21,
                              22,
                              22,
                              22,
                              22,
                              23,
                              23,
                              23,
                              23,
                              24,
                              24,
                              24,
                              24,
                              25,
                              25,
                              25,
                              25,
                              26,
                              26,
                              26,
                              26,
                              27,
                              27,
                              27,
                              27,
                              28,
                              28,
                              28,
                              28,
                              29,
                              29,
                              29,
                              29,
                              30,
                              30,
                              30,
                              30,
                              31,
                              31,
                              31,
                              31,
                              32,
                              32,
                              32,
                              32,
                              33,
                              33,
                              33,
                              33),
                    var2a = c(1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4,
                              1,
                              2,
                              3,
                              4),
                    var3a = c(113,
                              89,
                              99,
                              41,
                              72,
                              64,
                              39,
                              139,
                              135,
                              17,
                              3,
                              135,
                              63,
                              126,
                              34,
                              87,
                              84,
                              125,
                              123,
                              18,
                              115,
                              11,
                              68,
                              85,
                              48,
                              95,
                              56,
                              129,
                              41,
                              78,
                              82,
                              122,
                              124,
                              4,
                              60,
                              132,
                              67,
                              128,
                              46,
                              79,
                              110,
                              88,
                              19,
                              88,
                              88,
                              126,
                              30,
                              11,
                              52,
                              66,
                              15,
                              52,
                              6,
                              74,
                              14,
                              101,
                              88,
                              70,
                              58,
                              20,
                              104,
                              76,
                              134,
                              23,
                              40,
                              1,
                              47,
                              25,
                              49,
                              110,
                              96,
                              100,
                              106,
                              26,
                              93,
                              19,
                              87,
                              41,
                              13,
                              40,
                              63,
                              87,
                              137,
                              105,
                              89,
                              95,
                              24,
                              49,
                              112,
                              92,
                              45,
                              105,
                              112,
                              105,
                              114,
                              129,
                              84,
                              33,
                              95,
                              95,
                              15,
                              90,
                              1,
                              62,
                              20,
                              7,
                              18,
                              96,
                              4,
                              71,
                              42,
                              94,
                              45,
                              102,
                              55,
                              98,
                              124,
                              80,
                              76,
                              97,
                              41,
                              31,
                              25,
                              21,
                              135,
                              138,
                              121,
                              93,
                              17,
                              13,
                              49,
                              26))

这是我正在使用的代码:

#Open packages
library(ggplot2)
library(ggpubr)

#Set the theme
theme_set(theme_pubr())

#Change necessary columns to factor
data1$var2 <- factor(data1$var2, levels = c(1,2,3,4))
data2$var2a <- factor(data2$var2a, levels = c(1,2,3,4))

#Generate the plots
#Generate plots
plot1 <- ggplot(data1, aes(x = var1, y = var3, group = var2)) +
  geom_line(size = 1.5, aes(linetype = var2, color = var2)) +
  xlab('x_label') +
  ylab('y_label')+
  scale_fill_discrete(name = 'customized legend')

plot2 <- ggplot(data2, aes(x = var1a, y = var3a, group = var2a)) +
  geom_line(size = 1.5, aes(linetype = var2a, color = var2a)) +
  xlab('x_label') +
  ylab('y_label')+
  scale_fill_discrete(name = 'customized legend')

#Combine both into one picture
fig <- ggarrange(plot1, plot2,
                 ncol = 2,
                 nrow = 1,
                 common.legend = TRUE,
                 legend = "bottom")
fig

因为你没有在你的 ggplot 中使用 fill 美学,你不应该使用 scale_fill_discrete。您需要将 linetypecolor 的图例标题设置为“自定义图例”,因为这些是您使用的美学。

library(ggplot2)
library(ggpubr)

plot1 <- ggplot(data1, aes(x = var1, y = var3, group = var2)) +
  geom_line(size = 1.5, aes(linetype = var2, color = var2)) +
  xlab('x_label') +
  ylab('y_label') +
  labs(linetype = "customized legend", color = "customized legend")

plot2 <- ggplot(data2, aes(x = var1a, y = var3a, group = var2a)) +
  geom_line(size = 1.5, aes(linetype = var2a, color = var2a)) +
  xlab('x_label') +
  ylab('y_label') +
  labs(linetype = "customized legend", color = "customized legend")

#Combine both into one picture
ggarrange(plot1, plot2,
                 ncol = 2,
                 nrow = 1,
                 common.legend = TRUE,
                 legend = "bottom")