Dario.gs
Dario.gs

Reputation: 65

add sub/superscripts to barplot labels

I have this plot

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)

And I'd like to add sub- or super-scripts to the axis ticks; i.e.: "BRCA^a", "BLCA^a, b" and "ACC^b". But I am stuck since I don't know how to change the axis ticks without altering the data table. Is there a way to create a vector and use its elements as the axis ticks? in that case, I guess I could use something like "as.expression".

So here are the library and data:

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)

Upvotes: 2

Views: 899

Answers (1)

Felipe Alvarenga
Felipe Alvarenga

Reputation: 2652

Just add this one more command to your ggplot

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

This will change the ticks in the x axis

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")))

enter image description here

Upvotes: 1

Related Questions