So both lines of ggplot gets about the same graph, but which one is preferred? I normally see either one or another, but I couldn't find an explicit comparison between the two. Any light shined on this would be appreciated, thanks!
library(ggplot2)
ggplot(cars, aes(x=dist, y=speed))+geom_line()+labs(x='Distance travelled in m', y=expression(paste('Speed in' * m^2)))
ggplot(cars, aes(x=dist, y=speed))+geom_line()+labs(x='Distance travelled in m', y=bquote('Speed in' * m^2))
edit: I realised I forgot a space after 'in' in ylab, ignore that mistake...
expression('Speed in' ~ m^2)
~
produces a space and different arguments to paste
are separated by ,
(but paste
is not needed here). See help("plotmath")
.
bquote
is a different beast. It let's you do this:
unit <- quote(m^2)
ggplot(cars, aes(x=dist, y=speed))+
geom_line()+
labs(x='Distance travelled in m',
y=bquote('Speed in' ~ .(unit)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With