geom_text question

132 views
Skip to first unread message

AC Del Re

unread,
Dec 5, 2009, 9:04:17 PM12/5/09
to ggp...@googlegroups.com
Dear All,

I am interested in better understanding how to insert text (and additional data) into a plot. For example,

x<-rnorm(1:20)
y<-x+rnorm(1:20)
plot(x,y)
xrng<-range(x)  
yrng<-range(y) 
data1<-qplot(x,y)
caption<-paste(strwrap("Adj.means:  first= .111  second= .222 
 third= .333",15),collapse="\n")                    #this setup seems messy
data1+geom_text(aes(x,y,label=caption),
 data=data.frame(x=xrng[2],y=yrng[1]), 
 hjust=2,vjust=-2,size=3)

How can I get the text to be lined up and uniform? 

#like this
Adj. means:
 first =      .111
 second = .222
 third =     .333

Also, is there an easy way to enter in tables of data (such as the beta coefficients from lm(y~x)) but in a way where the coefficient names are included and where the line breaks can be specified to my liking?

reg<-lm(y~x)
beta1<-reg$coefficients[1:2]
caption<-paste(strwrap(beta1,collapse="\n")                    
data1+geom_text(aes(x,y,label=caption),
 data=data.frame(x=xrng[2],y=yrng[1]), 
 hjust=2,vjust=-2,size=3)

Finally, is there additional documentation on geom_text? I have read Hadley's book and perused the ggplot2 website and am interested in learning more.

Thank you,

AC

baptiste auguie

unread,
Dec 6, 2009, 3:04:20 AM12/6/09
to AC Del Re, ggp...@googlegroups.com
Hi,

you might find this Grid function convenient to display a data.frame,

library(gridExtra) # http://code.google.com/p/gridextra/
grid.table(as.data.frame(round(beta1,3)), cols=expression(beta[1]))

It could be adapted into a geom for ggplot2 (restricted to use with annotate).
By the way, annotate() is a convenient way to add text in ggplot2
without having to construct a dummy data.frame (it does it for you).

HTH,

baptiste
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2
output-table.pdf

baptiste auguie

unread,
Dec 6, 2009, 3:25:53 PM12/6/09
to AC Del Re, ggp...@googlegroups.com
Hi,

I wrote a basic geom_table that one can use with annotate to add a
table at a specific location in a ggplot. The table is typically drawn
from a data.frame but it may contain only a single row/column. I
defined some specific lists of settings for the appearance,
unfortunately these themes appear quite redundant with ggplot2's own
theming system because ggplot2 does not define theme elements for the
geoms.

library(ggplot2)
library(ggExtra) # http://code.google.com/p/ggextra/
x<-rnorm(1:20)
y<-x+rnorm(1:20)

xrng<-range(x)
yrng<-range(y)

p <- qplot(x,y)
caption <- as.data.frame(strwrap("Adj.means: first= .111 second=
.222 third= .333",15))

p + annotate("table", table=caption, x=1, y=-2, theme=theme.list()) + theme_bw()


reg<-lm(y~x)
beta1<-reg$coefficients[1:2]
caption <- as.data.frame(round(beta1,3))
p + annotate("table", table=caption, x=1, y=-2,
theme=theme.list(show.rownames=T, show.colnames=T),
cols=expression(beta[1]))


HTH,

baptiste
adj-output.pdf
lm-output.pdf
Reply all
Reply to author
Forward
0 new messages