我想用命名向量替换图中的 x 轴标签。我想在一些新名称中添加下标字符。我在下面给出了一个示例,其中我希望“H2O2 500 µM”为“H2O2 500 µM”。
我读过这可以通过将替换文本包含在
expression()
中来完成,但是当使用命名向量来替换名称时,这似乎不起作用。
这是一个简化的演示。
demo_data <- structure(list(treatment_duration_weeks = c(9, 9, 9, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15), treatment = c("negative_control", "negative_control",
"negative_control", "treatment1", "treatment1", "treatment1",
"treatment2", "treatment2", "treatment2", "treatment3", "treatment3",
"treatment3", "treatment4", "treatment4", "treatment4", "H2O2-500uM",
"H2O2-500uM", "H2O2-500uM", "negative_control", "negative_control",
"treatment1", "treatment1", "treatment2", "treatment2", "treatment3",
"treatment3", "treatment4", "treatment4", "H2O2-500uM", "H2O2-500uM"
), level = c(100, 100, 100, 107, 148, 126, 112, 160, 131, 112,
159, 122, 113, 170, 130, 54, 64, 24, 101, 100, 105, 190, 96,
170, 84, 176, 98, 171, 53, 59)), row.names = c(NA, -30L), class = c("tbl_df",
"tbl", "data.frame"))
treatment_rename <- c("negative_control" = "NC",
"treatment1" = "Treatment 1",
"treatment2" = "Treatment 2",
"treatment3" = "Treatment 3",
"treatment4" = "Treatment 4",
"H2O2-500uM" = "H2O2 500 µM")
ggplot(demo_data, aes(x = treatment, y = level)) +
facet_wrap(~ treatment_duration_weeks) +
stat_summary(aes(fill = treatment),
fun = mean, colour = "black", geom = "bar") +
geom_jitter(aes(fill = treatment),
shape = 21, colour = "black",
width = 0.1, height = 0,
size = 3) +
stat_summary(fun.data = mean_se,
geom = "errorbar", width = 0.2) +
scale_x_discrete(labels = treatment_rename) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
legend.position = "none")
我已经尝试过类似的帖子,但无法使其正常工作。
一种选择是使用
scales::label_parse()
,这当然仍然需要创建正确的数学表达式的向量:
library(ggplot2)
treatment_rename <- c(
"negative_control" = "NC",
"treatment1" = "Treatment~1",
"treatment2" = "Treatment~2",
"treatment3" = "Treatment~3",
"treatment4" = "Treatment~4",
"H2O2-500uM" = "H[2]*O[2]~500~µM"
)
ggplot(demo_data, aes(x = treatment, y = level)) +
facet_wrap(~treatment_duration_weeks) +
stat_summary(aes(fill = treatment),
fun = mean, colour = "black", geom = "bar"
) +
geom_jitter(aes(fill = treatment),
shape = 21, colour = "black",
width = 0.1, height = 0,
size = 3
) +
stat_summary(
fun.data = mean_se,
geom = "errorbar", width = 0.2
) +
scale_x_discrete(labels = \(x) scales::label_parse()(treatment_rename[x])) +
theme_bw() +
theme(
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
legend.position = "none"
)
第二个选择是使用 unicode 下标:
treatment_rename <- c(
"negative_control" = "NC",
"treatment1" = "Treatment 1",
"treatment2" = "Treatment 2",
"treatment3" = "Treatment 3",
"treatment4" = "Treatment 4",
"H2O2-500uM" = "H\u2082O\u2082 500 µM"
)
ggplot(demo_data, aes(x = treatment, y = level)) +
facet_wrap(~treatment_duration_weeks) +
stat_summary(aes(fill = treatment),
fun = mean, colour = "black", geom = "bar"
) +
geom_jitter(aes(fill = treatment),
shape = 21, colour = "black",
width = 0.1, height = 0,
size = 3
) +
stat_summary(
fun.data = mean_se,
geom = "errorbar", width = 0.2
) +
scale_x_discrete(labels = treatment_rename) +
theme_bw() +
theme(
axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),
legend.position = "none"
)