Home > Software engineering >  How can I edit the common legend title name using ggplot2 and ggpubr?
How can I edit the common legend title name using ggplot2 and ggpubr?

Time:02-24

I am using ggpubr to combine multiple graphs in a single plot, but cannot seem to correctly generate one graph with the title that I would like. I would like the title to say "Customized legend," given that it is a common legend for both graphs. Does anybody know how I can do this?

Here is my data:

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))

Here is the code that I am using:

#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

enter image description here

CodePudding user response:

Since you didn't use the fill aesthetic in your ggplot, you should not use scale_fill_discrete. What you need is to set the legend title of linetype and color to "customized legend", since those are the aesthetics that you used.

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")

customized legend

  • Related