Saving trailing zeros with plotmath

I use annotate() to overlay text on one of my ggplot2 . I use the parse=T option because I need to use the Greek letter rho. I want the text to say = -0.50 , but the trailing zero is truncated, and instead I get -0.5 .

Here is an example:

 library(ggplot2) x<-rnorm(50) y<-rnorm(50) df<-data.frame(x,y) ggplot(data=df,aes(x=x,y=y))+ geom_point()+ annotate(geom="text",x=1,y=1,label="rho==-0.50",parse=T) 

Does anyone know how I can get the last 0 to appear? I thought I could use paste() as follows:

 annotate(geom="text",x=1,y=1,label=paste("rho==-0.5","0",sep=""),parse=T) 

but then I get the error:

 Error in parse(text = lab) : <text>:1:11: unexpected numeric constant 1: rho==-0.5 0 ^ 
+7
source share
1 answer

This is a problem parsing a plotmath expression; it is not ggplot2 .

What you can do is make sure 0.50 interpreted as a character string, not a numeric value to be rounded:

 ggplot(data=df, aes(x=x, y=y)) + geom_point() + annotate(geom="text", x=1, y=1, label="rho=='-0.50'", parse=T) 

You will get the same behavior using base :

 plot(1, type ='n') text(1.2, 1.2, expression(rho=='-0.50')) text(0.8, 0.8, expression(rho==0.50)) 

If you want to use a more general approach, try something like

 sprintf('rho == "%1.2f"',0.5) 

There is r-help thread related to this problem.

+14
source

All Articles