Search code examples
rggplot2plotmath

Multiple indices in ggplot label


I would like to add a label containing multiple indices to a bar plot:

data<-as.data.frame(c("A"))
colnames(data)<-"A"
data$B<-5

ggplot(data, aes(x=A, y=B)) +
  geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) +
  annotate("text", x=1, y=2.5, label="some text")

I need to replace "some text" with "a1 a2 a3" where the numbers are in subscript. I tried the following but get errors:

annotate("text", x=1, y=0.4, parse=T, label=paste("a[1]","a[2]","a[3]",sep="     "))

Solution

  • The following does that:

    ggplot(data, aes(x=A, y=B)) + geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) + annotate("text", x=1, y=2.5, label="~a[1]~a[2]~a[3]", parse=TRUE)