2

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

Axeman
  • 32,068
  • 8
  • 81
  • 94
Dasr
  • 777
  • 6
  • 16
  • 1
    Does this post help: https://stackoverflow.com/questions/17334759/subscript-letters-in-ggplot-axis-label Or this: https://stackoverflow.com/questions/6202667/how-to-use-subscripts-in-ggplot2-legends-r – Ryan Morton Jan 24 '17 at 20:43
  • You may find [this answer](http://stackoverflow.com/a/19507920/2461552) useful, as it shows how to use expressions in the `labels` argument of `scale_color_manual'. – aosmith Jan 24 '17 at 23:38
  • Try this: `+labs(y=bquote(treatment[1]))` – J.Con Jan 25 '17 at 01:10

1 Answers1

2

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])))
Dasr
  • 777
  • 6
  • 16