将包含上标文本的后缀添加到 ggplot 中的轴文本
Adding a suffix that includes superscript text to axis text in ggplot
我想让我的 x 轴显示比例,并使用 scales::label_number()
函数在每个轴文本的末尾添加“m2”。可重现代码:
tibble(x = 1:10, y = 1:10) |>
ggplot(aes(x, y)) +
scale_x_continuous(label = scales::label_number(suffix = "m^2"))
它在 x 轴上给出了“5m^2”(例如),而我希望它显示“5m2”。 bquote()
和 expression()
的常见嫌疑人似乎不适用于 scales::label_number()
函数。
使用 scales::label_parse
你可以:
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"))
)
对 ^2 使用 unicode:“m\u00B2”或 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"))
由 reprex package (v2.0.1)
于 2022-05-16 创建
我想让我的 x 轴显示比例,并使用 scales::label_number()
函数在每个轴文本的末尾添加“m2”。可重现代码:
tibble(x = 1:10, y = 1:10) |>
ggplot(aes(x, y)) +
scale_x_continuous(label = scales::label_number(suffix = "m^2"))
它在 x 轴上给出了“5m^2”(例如),而我希望它显示“5m2”。 bquote()
和 expression()
的常见嫌疑人似乎不适用于 scales::label_number()
函数。
使用 scales::label_parse
你可以:
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"))
)
对 ^2 使用 unicode:“m\u00B2”或 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"))
由 reprex package (v2.0.1)
于 2022-05-16 创建