rggplot2plotaxis-labelssuperscript

output ggplot figure has unadjusted y axis label because of superscript of power of 10


I used the code below in my ggplot code because I would like to have the label "Content" and the power of 10^-3 (µg/mg DW) in two different lines of the y axis label.

labs(y=expression(paste("content\n (.", 10^{-3}," µg/mg DW)")))

The output is the following:

enter image description here

How can I make the labels centered and avoid this unnecessary created space in the label ? I tried changing the text size or image size but it didn't help.


Solution

  • One option would be to use atop() to create two separate lines:

    library(ggplot2)
    
    ggplot() +
      labs(y = ~atop(content, (10^{-3}~µg/mg~DW)))
    

    Another option would be to use ggtext::element_markdown which allows for styling text using some limited HTML and CSS, i.e. you can use a <sup> tag to create your superscript.

    ggplot() +
      labs(y = paste("content<br>(10<sup>-3</sup> µg/mg DW)")) +
      theme(axis.title.y = ggtext::element_markdown())