如何在R中添加下标

问题描述 投票:0回答:1

我正在 R 中格式化一些图表的标签。这是我用来格式化我的点和标签的代码。

point_4 <- list(scale_shape_manual(
  values = c(18, 15, 18, 17),
  name = "Treatment",
  labels = c("Control", "MET 0-8wks", "MET 0-4wks", "MET 4-8wks")
))

我用它来制作“line_graph_4_theme2()”,我应用它并且效果很好:

graph <- ggplot(data=mydata, aes(x=weeks, y=bw, group = rx, color = rx, linetype = rx, shape = rx))+
  stat_summary(fun = "mean", geom = "line", lwd = rel(1))+
  stat_summary(fun = mean,
               geom = "pointrange",
               fun.max = function(x) mean(x) + sd(x) / sqrt(length(x)),
               fun.min = function(x) mean(x) - sd(x) / sqrt(length(x)))+
  stat_summary(fun = "mean", geom = "point", size = rel(2), fill = "white", stroke = rel(1.1))+
  ggtitle("Title")+
  labs(subtitle = " ", x = " Age (Weeks) ", y = "Body Mass (g)", color = "rx") +
  scale_x_discrete(labels = c("5","6", "7", "8", "9","10", "11", "12", "13", "14","15","16","17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27"))+
  line_graph_4_theme2()
graph

此代码为我提供了一个图表,其中每组都有正确的标签“Control”、“MET 0-8wks”、“MET 0-4wks”、“MET 4-8wks”。

我希望能够为其他组的标签名称 MET0-8wks 等添加下标,这可能吗?

我见过使用的例子

main=expression('title'[2])

我只是不知道如何将其添加到我的行中:

  labels = c("Control", "MET 0-8wks", "MET 0-4wks", "MET 4-8wks")

r plot label
1个回答
0
投票

您需要为 labels 参数提供一个表达式对象向量:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.3

my_label_expressions <- c(
  "Control",
  expression("MET"[0-8+"wks"]),
  expression("MET"[0-4+"wks"]),
  expression("MET"[4-8+"wks"])
)

data.frame(
  x = rnorm(100),
  y = rnorm(100),
  s = sample(LETTERS[1:4], 100, TRUE)
) |>
  ggplot(aes(x, y, shape = s)) + 
  geom_point() + 
  scale_shape_manual(values = 1:4, labels = my_label_expressions)

创建于 2024-04-12,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.