添加 sub/superscripts 到条形图标签

add sub/superscripts to barplot labels

我有这个剧情

plot.colors<- c("#a6cee3", "#1f78b4","#b2df8a","#33a02c", "#525252")
ggplot() + 
  geom_bar(aes(y = v3, x = v1 , fill = v2), data = melted.data, stat="identity") + 
  coord_flip() + 
  scale_fill_manual(values= plot.colors) + 
  facet_wrap(~v4)

而且我想为轴刻度添加子脚本或上脚本;即:"BRCA^a"、"BLCA^a, b" 和 "ACC^b"。 但是我被卡住了,因为我不知道如何在不改变数据的情况下改变轴刻度 table。有没有办法创建一个向量并将其元素用作轴刻度?在那种情况下,我想我可以使用 "as.expression".

之类的东西

这里是图书馆和数据:

library(ggplot2)

v1<- c("ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA", "ACC", "BLCA", "BRCA")
v2<- c("LumA", "LumA", "LumA", "LumA", "LumA", "LumA", "LumB", "LumB", "LumB", "LumB", "LumB", "LumB", "Basal", "Basal", "Basal", "Basal", "Basal", "Basal", "Her2", "Her2", "Her2", "Her2", "Her2", "Her2", "NoA", "NoA",  "NoA")
v3<- c(46.575342, 39.726027, 36.120401, 26.027397, 24.931507, 22.965440, 9.589041, 14.794521, 22.073579, 1.369863,  2.191781,  7.580825, 26.027397, 31.506849, 26.198439,  2.739726, 4.931507, 22.742475, 17.808219, 13.972603, 15.607581, 4.109589,  3.013699, 10.256410, 65.753425, 64.931507, 36.454849)
v4<- c("pam50", "pam50", "pam50", "pbcmc", "pbcmc", "pbcmc", "pam50", "pam50", "pam50", "pbcmc", "pbcmc", "pbcmc", "pam50", "pam50", "pam50", "pbcmc", "pbcmc", "pbcmc", "pam50", "pam50", "pam50", "pbcmc", "pbcmc", "pbcmc", "pbcmc", "pbcmc", "pbcmc")

melted.data<- data.frame(v1, v2, v3, v4)
rm(v1, v2, v3, v4)

只需将这一条命令添加到您的 ggplot

scale_x_discrete(labels = parse(text=c("BRCA" = "BRCA^a", "BLCA" = "BLCA^{list(a, b)}", "ACC" = "ACC^b")))

这将改变 x 轴上的刻度

ggplot() + 
  geom_bar(aes(y = v3, x = v1 , fill = v2), data = melted.data, stat="identity") + 
  coord_flip() + 
  scale_fill_manual(values= plot.colors) + 
  facet_wrap(~v4) +
  scale_x_discrete(labels = parse(text=c("BRCA" = "BRCA^a", "BLCA" = "BLCA^{list(a, b)}", "ACC" = "ACC^b")))