0

This is my first post, so I apologise if I've missed something. I'm making a plot using ggplot on R. I would like the units of "log FMRm" to be on a new line in the legend title. How can I get around this?

enter image description here

ggplot(data = cf_joint2, aes(j_df.d13C_VPDB, j_df.d15N_Air)) +
  geom_point(aes(shape= groupmass, color = j_df.log_mass_scaled), size= 1.5) +
  labs(x=(expression(paste(delta^{13}, "C (\u2030)"))),
       y=(expression(paste(delta^{15}, "N (\u2030)"))),
       colour= expression(paste("log FMR"[m]~"\n(mg"~O[2]~kg^-1~hr^-1*")")),
       shape="log Mass (kg)") +
  theme(text = element_text(size=15)) +
  scale_colour_viridis_c()+
  scale_shape_manual(values=c(16,17,15,18))+
  stat_ellipse(level=0.99) +
  facet_wrap(~factor(j_df.Common_name)) 

I've tried using \n, but am not getting the right results. I had a go at using atop(), but it made the text lower but not left-aligned.

enter image description here

ggplot(data = cf_joint2, aes(j_df.d13C_VPDB, j_df.d15N_Air)) +
  geom_point(aes(shape= groupmass, color = j_df.log_mass_scaled), size= 1.5) +
  labs(x=(expression(paste(delta^{13}, "C (\u2030)"))),
       y=(expression(paste(delta^{15}, "N (\u2030)"))),
       colour= expression(paste(atop("log FMR"[m]), "(mg"~O[2]~kg^-1~hr^-1*")"), hjust =0 ),
       shape="log Mass (kg)") +
  theme(text = element_text(size=15)) +
  scale_colour_viridis_c()+
  scale_shape_manual(values=c(16,17,15,18))+
  stat_ellipse(level=0.99) +
  facet_wrap(~factor(j_df.Common_name)) 
r2evans
  • 141,215
  • 6
  • 77
  • 149
Max
  • 1
  • 1
  • 2
    To clarify, your use of `atop` fails because you have to give it two arguments. In your case, by pasting, `atop` only gets the first argument, and so it puts `log FMR` on top with nothing below it, then the other line simply next to it. – Axeman May 15 '23 at 18:32
  • @Axeman Is there a way to make both layers left-aligned rather than center-aligned? Thanks. – Max May 15 '23 at 18:47
  • Not to my knowledge. – Axeman May 16 '23 at 03:39

1 Answers1

2

We can use atop here:

ggplot(mtcars, aes(disp, mpg, color = cyl)) +
  geom_point() +
  labs(colour = expression(atop('log FMR'[m],
                                '(mg' ~ O[2] ~ kg^-1 ~ hr^-1 * ')' )))

legend with embedded newline in expression legend name

atop is described in ?plotmath.

r2evans
  • 141,215
  • 6
  • 77
  • 149