Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot italicize part of legend across multiple lines

I am trying to italicize part of my legend elements. I have been able to get the subsets I want italicized using the answer in this OP, but this then creates problems with how the text shows up.

Here is the data (slightly modified):

Behaviour   Repeatability   Age stage
Activity    0.1             PE  A
Activity    0.5             PE2 A
Activity    0.5             PW  B
Activity    0.5             A   C
Activity    0.4             A2  C
Activity    0.4             PW1 B
Aggression  0.2             PE  A
Aggression  0.3             PE2 A
Aggression  0.3             PW  B
Aggression  0.4             A   C
Aggression  0.3             A2  C
Aggression  0.0             PW1 B

Here is the plot (without the italics): enter image description here

When I try to italicize the legend text that are in brackets based on this solution, the plot gets messed up:

enter image description here

The code I use to get this plot is below (minus unneeded formatting):

ggplot(df, aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age, linetype=Age)) + 
    geom_point() + 
    scale_colour_manual(values = c("#D55E00", "#0072B2", "black", "black", "#CC79A7", "black"), name = "Study", breaks=c("A","A2", "PE", "PE"=="PE2", "PW", "PE"=="PW1"),
    labels=c(
    (expression("Taylor et al. 2012", paste(italic("\n 366 adults")))),
    (expression("Boon et al. 2008", paste(italic("\n (71 adult females)")))), 
    (expression("This study", paste(italic("\n (102 juveniles)")))),
    "This study", 
    (expression("Kelley et al. 2015", paste(italic("\n (16 yearlings)")))), 
    "This study")) +
  theme(legend.key.size = unit(2.5, 'lines'))
like image 724
Blundering Ecologist Avatar asked Jul 23 '19 16:07

Blundering Ecologist


2 Answers

Like mentioned by axeman in the commentary: Multi-line expressions are not well supported. If you use expression with \n you'll get adjustment problems. This happens, since the ggplot works with the grid maintainers (check this git issue).

You can linebreak in plot's with atop, which is a r base function and also works for ggplot. The following code is adjusted for your case. The expression is still needed for using italic.

library(ggplot2)
ggplot(df, aes(x = stage, y = Repeatability, shape = Behaviour, colour=Age, linetype=Age)) + 
  geom_point() + 
  scale_colour_manual(values = c("#D55E00", "#0072B2", "black", "black", "#CC79A7", "black"), name = "Study", breaks=c("A","A2", "PE", "PE"=="PE2", "PW", "PE"=="PW1"),
                      labels=c(
                        (expression(atop("Taylor et al. 2012", italic("(366 adults)")))),
                        (expression(atop("Boon et al. 2008", italic("(71 adult females)")))), 
                        (expression(atop("This study", italic("(102 juveniles)")))),
                        "thisstudy",
                        (expression(atop("Kelley et al. 2015", italic("(16 yearlings)")))),
                        "thisstudy"))+
  theme(legend.key.size = unit(2.5, 'lines'))

the output plot is as follows output

By default your labels get centered now, since atop creates a formula expression.

If you want to use your expression with paste you can do it like this:

(expression(paste("Taylor et al. 2012 \n", italic("(366 adults)"))))

but this occurs in wrong adjustment of the linebreak.

like image 173
mischva11 Avatar answered Sep 29 '22 18:09

mischva11


Maybe you can change the alignment with this:

legend.text.align = 

where 1 is right and 0 is left.

like image 30
Lieke Avatar answered Sep 29 '22 16:09

Lieke