How Can I add a Textbox to a plot created with ggplot2?

5,776 views
Skip to first unread message

Michael M

unread,
Jul 7, 2010, 9:31:58 AM7/7/10
to ggplot2
How Can i add a Textbox to a plot created with ggplot2?

I want to create a Plot that has a second Legend like box in which i
can write free text.

I want to use that box to plot some numbers like median mean etc.

Cheers,

Michael

Hadley Wickham

unread,
Jul 7, 2010, 1:55:36 PM7/7/10
to Michael M, ggplot2
Hi Michael,

You can use geom_text to add text positioned in data units, or
grid.text to add text positioned in absolute units.

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Michael Martin

unread,
Jul 7, 2010, 4:27:16 PM7/7/10
to Hadley Wickham, ggplot2
Thanks a lot for the answer.
Is there an easy way to add a box around that text?
And is there a way to make parts of the text look bold?

Hadley Wickham

unread,
Jul 16, 2010, 6:46:48 AM7/16/10
to Michael Martin, ggplot2
> Is there an easy way to add a box around that text?
> And is there a way to make parts of the text look bold?

Not with geom_text, but you have total control if you use grid.
There's a good example in the R graphics book.

Hadley

baptiste auguie

unread,
Jul 17, 2010, 8:02:21 AM7/17/10
to Michael Martin, ggplot2
You could also try this,

library(ggExtra)
# and version 0.6.5 of gridExtra

p = qplot(1:2, 1:2) + theme_bw()

d <- data.frame(my.values = c( "x=12", "y=1.34"))

p + annotate("table", x=1.2, y=1.5, table=d, just=c("left", "top"),
theme=theme.list(show.box = TRUE, separator = "black",
show.csep = TRUE, show.rsep = TRUE, show.colnames=T))

HTH,

baptiste

Picture 11.png

Felipe Carrillo

unread,
Jul 17, 2010, 3:12:40 PM7/17/10
to baptiste auguie, Michael Martin, ggplot2
How do I download gridExtra 0.6.5 from trunk?
It appears that the latest version is 0.6.1 in R-forge 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

Felipe Carrillo

unread,
Jul 17, 2010, 4:26:58 PM7/17/10
to baptiste auguie, Michael Martin, ggplot2
I was able to download it to my desktop from here:
 
http://code.google.com/p/gridextra/updates/list

but after I decompress the file and replace it in my library, it
still says that the version is 0.6.1 (sessionInfo()) and the new function
to apply the argument just=c("left","top") doesn't work

baptiste auguie

unread,
Jul 17, 2010, 5:31:42 PM7/17/10
to Felipe Carrillo, ggplot2
The source package is at googlecode, you can get it with svn or in the
download list. You seem to have installed it incorrectly, somehow.

The code will also get transferred to r-forge by tomorrow, and if it
passes the checks there (as it should) I'll update the CRAN version
too.

baptiste

Reply all
Reply to author
Forward
0 new messages