Superscan in ggplot2 using plotmath

I am trying to get a superscript for units of my y axis using the plotmath expressions mentioned in several posts. I looked at the streams using the expression as well here , but it just doesn't work for me, Here is what I still have:

ggplot(data.frame, aes(Region, Zmar_sh)) + geom_boxplot() + xlab("Region") + ylab(expression(paste("eelgrass shoot density", " (shoots/", expression(m[2])))) + ggtitle(expression(paste(italic("Zostera marina")," shoot density in X Bay", sep = " "))) 

The ggtitle effect works, but I still do not get my y axis to read "density of shoots of elgrass (shoots / m2)" with the inscription "2" as a superscript. What am I missing?

+5
source share
1 answer

You do not need a second expression() . In addition, m[2] gives an index. Use m^2 for superscript.

Here is an example with the mtcars embedded data mtcars :

 ggplot(mtcars, aes(wt, mpg)) + geom_point() + ylab(expression(paste("eelgrass shoot density", " (shoots/", m^2,")"))) 

In fact, you don’t even need to use paste . Instead, you can do this:

 ylab(expression(eelgrass~shoot~density~(shoots/m^2))) 

where ~ is the space character. If you put actual spaces between words (without using ~ ), you will get an error message. However, for readability, you can also do this:

 ylab(expression(eelgrass ~ shoot ~ density ~ (shoots/m^2))) 

which is equivalent to the code above, since spaces are ignored as long as ~ exists.

In any of the above ylab expressions, the following graph will be shown:

enter image description here

+4
source

Source: https://habr.com/ru/post/1244694/


All Articles