1

I have encountered problem in putting subscript in plot Text. First I did simple linear normal regression of 2 continuous variable and plotted it using ggplot.

Olm=lm(LPBEO ~ SNTO, LPBESNTR)
Ointercept=signif(coef(Olm)[1], digits=3)
Oslope=signif(coef(Olm)[2], digits=3)
Otext= paste("O-LPBE[50]","=",Ointercept,"+ (", Oslope, "x O-SN[50])")
Opred<- predict(Olm, interval="prediction")
Odataframe=data.frame(cbind(LPBESNTR$SNTO,LPBESNTR$LPBEO,Opred))
colnames(Odataframe)=c("SNTO", "LPBEO", "fit", "lwr", "upr")
O=ggplot(Odataframe, aes(x=SNTO, y=LPBEO))+
  geom_point(shape=20,colour = "red", size = 3)+
  geom_line(aes(y=lwr), color = "red", linetype = 2, size=1)+
  geom_line(aes(y=upr), color = "red", linetype = 2, size=1)+
  geom_smooth(method=lm, fill = "orange",colour="red", size = 1 )+
  ggtitle("SNT O vs LPBE O") + 
  theme(plot.title = element_text(face="bold",size=20)) + 
  xlab(bquote(~"Log"[10]~ "SN"[50]))+ 
  theme(axis.title.x = element_text(face='bold', size=20),axis.text.x = element_text(face='bold', size=20)) +
  ylab(bquote(~"Log"[10]~"LPBE"[50]))+
  theme(axis.title.y = element_text(face='bold', size=20),axis.text.y  = element_text(face='bold', size=20)) + 
  annotate("text", x = 1.5, y = 4.5, label = Otext, color="black", fontface="bold", size = 5, parse=FALSE)
O

I want to add subscript in text for display of regression equation. For that first I created a object Otext using paste command, which I used for annotating plot. the command creates object but not able to display subscripts in texts. I am not able to point out where is the issue in code.
I need 50 to appear as subscript in text annotation to the plot,

Otext= paste("O-LPBE[50]","=",Ointercept,"+ (", Oslope, "x O-SN[50])")

however it shows as such in brackets not as subscripts.

I request someone to clarify how to do this. Thank you

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
  • Possible duplicate of [ggplot2 - annotate outside of plot](http://stackoverflow.com/questions/12409960/ggplot2-annotate-outside-of-plot) – cuttlefish44 Sep 20 '16 at 08:32

1 Answers1

0

You can use expression() for the subscripts but because you want to reference a variable as well we need to use bquote():

Otext=bquote(paste("O-LPBE"[50],"=",.(Ointercept),"+ (", .(Oslope), " x O-SN"[50],")"))

Here we use .(variable) to reference your variables and [subscript] for the subscripts (^ for superscripts).

greghk
  • 91
  • 4
  • i have tried as mentioned above but i am getting error "Error: Aesthetics must be either length 1 or the same as the data (1): label, colour, size, fontface". – R.P. Tamil Selvan Sep 21 '16 at 10:26
  • Sorry i thought you wanted it as an axis label or title in which case it should be fine. To use Otext just as a label on the graph you'll need to use library(grid) and then my_grob = grobTree(textGrob(Otext, x=0.5, y=0.5)) and then add annotation_custom(my_grob) to your ggplot – greghk Sep 21 '16 at 11:15
  • with the existing code i am getting the text in plot, but the issue is in the text portion, in which i want to write 50 in subscript, i tried with 'expression(paste)', 'bquote(expression)', but both show some errors or not not showing text in plot. – R.P. Tamil Selvan Sep 21 '16 at 12:04
  • Otext=bquote(paste("O-LPBE"[50],"=",.(Ointercept),"+ (", .(Oslope), " x O-SN"[50],")")); my_grob = grobTree(textGrob(Otext, x=0.5,y=0.5)); ggplot(df, aes(x,y)) + geom_point() + annotation_custom(my_grob) – greghk Sep 21 '16 at 13:35
  • yes the 50 is in subscripts now but the text appear in middle of plot where the regressing line and points are there. i tried to change the my_grob = grobTree(textGrob(Otext, x=0.5,y=0.5))' parameter x=0.5, y=0.5 into 1.5 and 4.5 respectively but all have disappeared. i want the text to appear in upper left corner in bold face. – R.P. Tamil Selvan Sep 22 '16 at 10:59
  • i tried to changing like this "O_grob = grobTree(textGrob(Otext,x=0.25,y=0.9, just="centre"),gp = gpar(fontface="bold",fontsize=18)) " and got it now. thanks a lot – R.P. Tamil Selvan Sep 22 '16 at 11:11
  • the x and y arguments in textGrob refer to a standardized point in the plot, not a co-ordinate. This is so you can specify x = 0.1 and y = 0.9 (for upper left hand) and the text will always appear there, regardless of whether the plot area changes. So basically you specify x and y between 0 and 1 and that determines its position – greghk Sep 22 '16 at 11:18