Home > Mobile >  How to wrap legend text in ggplot?
How to wrap legend text in ggplot?

Time:03-17

This is the head of my data:

structure(list(aasta = c(2012, 2013, 2014, 2015, 2016, 2017, 
2018, 2019, 2020, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 
2020), tunnus1 = c("Ennetähtaegselt vanaduspensionile minejate arv", 
"Ennetähtaegselt vanaduspensionile minejate arv", "Ennetähtaegselt vanaduspensionile minejate arv", 
"Ennetähtaegselt vanaduspensionile minejate arv", "Ennetähtaegselt vanaduspensionile minejate arv", 
"Ennetähtaegselt vanaduspensionile minejate arv", "Ennetähtaegselt vanaduspensionile minejate arv", 
"Ennetähtaegselt vanaduspensionile minejate arv", "Ennetähtaegselt vanaduspensionile minejate arv", 
"Vanaduspensionile minejate arv", "Vanaduspensionile minejate arv", 
"Vanaduspensionile minejate arv", "Vanaduspensionile minejate arv", 
"Vanaduspensionile minejate arv", "Vanaduspensionile minejate arv", 
"Vanaduspensionile minejate arv", "Vanaduspensionile minejate arv", 
"Vanaduspensionile minejate arv"), vaartus1 = c(1022, 1173, 1129, 
1083, 1294, 1026, 862, 751, 764, 6529, 7683, 6797, 6892, 8106, 
6922, 6502, 6098, 5817), tunnus2 = c("Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Ennetähtaegselt vanaduspensionile minejate osakaal kõigist eelmisel aastal  VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest", 
"Vanaduspensionile minejate osakaal kõigist eelmisel aastal VTI-dest"
), vaartus2 = c(0.01, 0.011, 0.01, 0.01, 0.012, 0.009, 0.008, 
0.007, 0.007, 0.066, 0.073, 0.063, 0.064, 0.074, 0.062, 0.059, 
0.057, 0.054)), row.names = c(NA, -18L), class = c("tbl_df", 
"tbl", "data.frame"))

This is my plot:

ggplot(ennet1, aes(y = vaartus2, x = aasta, group=tunnus2, label=scales::percent(vaartus2,decimal.mark=',',accuracy = 0.1))) 
  geom_line(aes(color=tunnus2, linetype=tunnus2)) 
  geom_point(aes(color=tunnus2)) 
  geom_label(vjust = -0.6, label.size = 0, label.padding = unit(0.3, "lines"), size = 3.5) 
  scale_x_continuous(breaks = seq(2012,2020,by=2)) 
  scale_y_continuous(labels = scales::percent_format(decimal.mark = ',',accuracy=1), expand = expansion(mult = c(0, 0.5))) 
  cen_theme() 
  theme(
    strip.background =element_blank(),
    strip.text =element_text(size=11),
    axis.title.y = element_text(angle = 90), 
    axis.text.x = element_text(size = 11), 
    axis.text.y = element_text(size = 11), 
    legend.text = element_text(size = 11)) 
  scale_fill_manual(values = cen_cols) 
  scale_color_manual(values = cen_cols) 
  theme(legend.title = element_blank()) 
  theme(plot.title = element_text(size = 11, hjust=0.5)) 
  theme(axis.title.x = element_blank()) 
  theme(axis.title.y = element_blank()) 
  theme(legend.key.height=unit(2, "cm")) 
  theme(legend.position="top", legend.box="vertical", legend.margin=margin())

This is what the graph looks like right now:

enter image description here

As you can see the legend titles are too long and I would like to wrap them. I tried library(stringr) and adding str_wrap, but really I don't know where to put it and it did not work.

Thank you in advance!

CodePudding user response:

You could wrap your text using guide_legend with two rows and change size of text in legend.text. You can use this code:

library(scales)
library(tidyverse)
ggplot(ennet1, aes(y = vaartus2, x = aasta, group=tunnus2, label=scales::percent(vaartus2,decimal.mark=',',accuracy = 0.1))) 
  geom_line(aes(color=tunnus2, linetype=tunnus2)) 
  geom_point(aes(color=tunnus2)) 
  geom_label(vjust = -0.6, label.size = 0, label.padding = unit(0.3, "lines"), size = 3.5) 
  scale_x_continuous(breaks = seq(2012,2020,by=2)) 
  scale_y_continuous(labels = scales::percent_format(decimal.mark = ',',accuracy=1), expand = expansion(mult = c(0, 0.5))) 
  #cen_theme() 
  theme(
    strip.background =element_blank(),
    strip.text =element_text(size=11),
    axis.title.y = element_text(angle = 90), 
    axis.text.x = element_text(size = 11), 
    axis.text.y = element_text(size = 11), 
    legend.text = element_text(size = 11)) 
  #scale_fill_manual(values = cen_cols) 
  #scale_color_manual(values = cen_cols) 
  theme(legend.title = element_blank()) 
  theme(plot.title = element_text(size = 11, hjust=0.5)) 
  theme(axis.title.x = element_blank()) 
  theme(axis.title.y = element_blank()) 
  theme(legend.key.height=unit(2, "cm")) 
  theme(legend.position="top", legend.box="vertical", legend.margin=margin())  
  theme(legend.text=element_text(size=8))  
  guides(colour = guide_legend(nrow = 2))

Output:

enter image description here

CodePudding user response:

You could pass a function to the labels argument of the scale. To wrap your labels using strings::str_wrap you could use e.g. labels = ~ stringr::str_wrap(.x, width = 20).

Note 1: As you use three aesthetics you have to do that for each of your scales, i.e. color, fill and linetype. Otherwise the legends will not get merged.

Note 2: I dropped your theme and your colors and used scale_fill_discrete and scale_color_discrete instead. But the same works for scale_xxx_manual.

library(ggplot2)

ggplot(ennet1, aes(y = vaartus2, x = aasta, group = tunnus2, label = scales::percent(vaartus2, decimal.mark = ",", accuracy = 0.1)))  
  geom_line(aes(color = tunnus2, linetype = tunnus2))  
  geom_point(aes(color = tunnus2))  
  geom_label(vjust = -0.6, label.size = 0, label.padding = unit(0.3, "lines"), size = 3.5)  
  scale_x_continuous(breaks = seq(2012, 2020, by = 2))  
  scale_y_continuous(labels = scales::percent_format(decimal.mark = ",", accuracy = 1), expand = expansion(mult = c(0, 0.5)))  
  theme(
    strip.background = element_blank(),
    strip.text = element_text(size = 11),
    axis.title.y = element_text(angle = 90),
    axis.text.x = element_text(size = 11),
    axis.text.y = element_text(size = 11),
    legend.text = element_text(size = 11)
  )  
  scale_linetype_discrete(labels = ~ stringr::str_wrap(.x, width = 20))  
  scale_fill_discrete(labels = ~ stringr::str_wrap(.x, width = 20))  
  scale_color_discrete(labels = ~ stringr::str_wrap(.x, width = 20))  
  theme(legend.title = element_blank())  
  theme(plot.title = element_text(size = 11, hjust = 0.5))  
  theme(axis.title.x = element_blank())  
  theme(axis.title.y = element_blank())  
  theme(legend.key.height = unit(2, "cm"))  
  theme(legend.position = "top", legend.box = "vertical", legend.margin = margin())

  • Related