Home > Net >  Adding prime symbol (') to ggplot2 axis label using expression()
Adding prime symbol (') to ggplot2 axis label using expression()

Time:11-28

I have the following code snippet that should (once complete) plot u_prime and v_prime on the log2(x 1) scale using expression:

p <- ggplot(df_uv_prime, aes(x = u_prime, y = v_prime))   geom_point(colour = "blue")  
            labs(x = expression(log[2](u^{...}   1)), y = expression(log[2](v^{...}   1)))  
            xlim(0, 1)  
            ylim(0, 1)

However adding ' in place of ... doesn't work, as R expects a closing '.

Is there a base R solution without having to resort to any packages?

CodePudding user response:

Based on your description of the desired outcome, one option is to enclose the single quote or backtick (not sure which one you're using) in double quotes and add 'connectors' (*) or 'spaces' (~) to either side, e.g. backtick with space on x axis, single quote with connector on y axis:

library(ggplot2)

ggplot(mtcars, aes(x = hp, y = disp))  
  geom_point(colour = "blue")  
  labs(x = expression(log[2](u*"`"~   1)),
       y = expression(log[2](v*"'"*   1)))

Created on 2022-11-28 with reprex v2.0.2

  • Related