Search code examples
rggplot2subscript

Subscript in ggplot2


I am trying to get a combination of colours I want and legend labels I want with ggplot2 graph.

Essentially the code below achieves most things apart from I can't get the legend label to display what I want, ideally I would like it to be 'treatment 1' with '1' as subscript but I can't put an expression like this expression('treatment'[+1]) as it doesn't seem to accept it in the labels argument. Can someone help please it's driving me crazy!

I think it should be possible to insert it somewhere else in the syntax but I am unsure as to where.

graph1<-ggplot(data2, aes(SMR_residuals,y=log(Time), color = factor(Treatment, labels = c("1", "2", "3","4")))) + geom_point() + labs(color = "Treatment") + geom_smooth(method="lm", se=F) + scale_colour_brewer(palette = "Set1")


Solution

  • So I think with some help I managed to work it out, I ended up using scale_colour_brewer and writing the expression for the labels in there, this seems to have done the trick:

    graph1<- ggplot(data2,aes(SMR_residuals,y=log(Time),color=Treatment)) +
    geom_point()+ labs(color="Treatment") + geom_smooth(method="lm", se=F) +
    theme(legend.text.align = 0) + scale_colour_brewer(palette="Set1", breaks =
    c("IND","IND2","IND4","IND6"),
    labels=c("Treatment",expression("Treatment"[+2]),expression("Treatment"[+4]),
    expression("Treatment"[+6])))