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.
CodePudding user response:
Use unicode for ^2: "m\u00B2" or 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)
CodePudding user response:
Using scales::label_parse
you could do:
library(ggplot2)
data.frame(x = 1:10, y = 1:10) |>
ggplot(aes(x, y))
scale_x_continuous(
label = ~ scales::label_parse()(
paste0(scales::label_number()(.x), "*m^2"))
)