expression geom_text()

1,117 views
Skip to first unread message

blessedboy

unread,
Jul 25, 2010, 3:00:48 AM7/25/10
to ggplot2
hey all,

i just want to show an expression (R2=0.4) on the middle of my
plot . but i knew that i can't do this with the geom_text( ) instead i
have to use the annoate. but i failed to do it

would you please help me with the below codes how to show the
expression using annotate

d=data.frame(x=1:10,y=runif(10))
p=ggplot(d,aes(x,y))+geom_point()

tt=paste('R^2"=0.4) #### R the power of 2

p2=p+annotate("text",x= 5,y=0.5, label=tt, parse=TRUE)

print(p2)


Brandon Hurr

unread,
Jul 25, 2010, 7:42:41 AM7/25/10
to blessedboy, ggplot2
I pulled this together from some code that someone else helped me put together (In other words I don't fully understand everything that it going on... yet). It seems to work, but you have to specify the coordinates manually according to your data. 

d=data.frame(x=1:10,y=runif(10))
p=ggplot(d,aes(x,y))+geom_point()

m <- lm(y ~ x, data = d)

eq <- substitute(italic(r)^2~"="~r2, list(r2 = format(summary(m)$r.squared, digits = 3)))

dftext <- data.frame(x = 1, y = 1, eq = as.character(as.expression(eq)))

p + geom_text(aes(label = eq), data = dftext, parse = TRUE)



HTH, 

Brandon

On Sun, Jul 25, 2010 at 04:00, blessedboy <kah...@gmail.com> wrote:
d=data.frame(x=1:10,y=runif(10))
p=ggplot(d,aes(x,y))+geom_point()

Reply all
Reply to author
Forward
0 new messages