Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a suffix that includes superscript text to axis text in ggplot

I want to have my x-axis show the scale with an added "m2" at the end of each axis text using the scales::label_number() function. Reproducible code:

tibble(x = 1:10, y = 1:10) |> 
ggplot(aes(x, y)) + 
scale_x_continuous(label = scales::label_number(suffix = "m^2"))

which gives me "5m^2" (for example) on the x-axis, whereas I want it to show "5m2". It seems the usual suspects of bquote() and expression() do not work with the scales::label_number() function.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use unicode: "m\u00B2" or the unicode for m^2: "\u33A1"

library(tibble)
library(ggplot2)

tibble(x = 1:10, y = 1:10) |>  
  ggplot(aes(x, y)) +  
  scale_x_continuous(label = scales::label_number(suffix = "m\u00B2"))

Created on 2022-05-16 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading