ggplot italicize part of legend across multiple lines
Asked Answered
M

2

7

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'))
Miquelon answered 23/7, 2019 at 16:53 Comment(1)
Multi-line expression are not well supported, also see here: #13224346Dell
S
3

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.

Shudder answered 26/7, 2019 at 8:40 Comment(4)
The paste in the expression is not necessary.Carve
@Carve true, changed itShudder
Thank you! That solution does the trick. Is there a way to have the labels left justified instead of centered?Miquelon
@BlunderingEcologist I don't know any way to change it, sorryShudder
S
1

Maybe you can change the alignment with this:

legend.text.align = 

where 1 is right and 0 is left.

Severn answered 14/10, 2020 at 7:33 Comment(1)
That helps slightly (for the first line, but not the second)Miquelon

© 2022 - 2024 — McMap. All rights reserved.