Is there a way to add a grid.table() to a ggplot2 plot

1,401 views
Skip to first unread message

Michael M

unread,
Jul 10, 2010, 12:32:40 PM7/10/10
to ggplot2
Hi again,
Is there a way to add a grid table inside a ggplot2 plot?
http://rwiki.sciviews.org/doku.php?id=tips:graphics-grid:table

Is there an example somewhere?

Thanks in advance i hope someone knows the answer because grid.tables
look cool

baptiste auguie

unread,
Jul 11, 2010, 7:08:47 AM7/11/10
to Michael M, ggplot2
Hi,

A proof-of-principle is given in the ggExtra package,


require(gridExtra)
require(ggExtra)

dsmall <- diamonds[sample(nrow(diamonds), 100), ]
str(dsmall)
d <- ggplot(dsmall, aes(carat, price)) +
annotate("table", x=1, y=10000, table=head(iris[, 1:3], 3)) +
geom_point()
d

HTH,

baptiste

> --
> 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
>

Felipe Carrillo

unread,
Jul 11, 2010, 1:18:27 PM7/11/10
to baptiste auguie, Michael M, ggplot2
HI Bautiste:
How do you make the font and columns smaller to compact the table? Also,
trying to use 'Inf' to place the table on the top left corner hides the column
headers, is there a way to avoid this?
 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish & Wildlife Service
California, USA

baptiste auguie

unread,
Jul 11, 2010, 2:27:47 PM7/11/10
to Felipe Carrillo, ggplot2
Hi,


To change the size you can use the global gpar() of the tableGrob,

annotate("table", x=1, y=Inf, table=head(iris[, 1:3], 3), gp=gpar(cex=0.5))

and/or play with the individual components such as cell margins and
header text size etc. See the complete list of arguments in
?tableGrob.

I'm not sure why but the previous example fails for me with y=Inf. In
any case, the placement is relative to the center of the table (I
think), and I have not really thought of a way to specify hjust/vjust.
Since the table grob knows its actual size, in theory it should be
possible to use this information in an improved ggplot2 geom.

HTH,

baptiste

baptiste auguie

unread,
Jul 11, 2010, 4:10:11 PM7/11/10
to Felipe Carrillo, ggplot2
This seems to do the trick, I've committed the change,

GeomTable <- proto(Geom, {
objname <- "table"
desc <- "Custom table grob"

draw <- function(., data, scales, coordinates, table,
just=c("centre", "centre"), ...) {
with(coordinates$transform(data, scales), {
g <- tableGrob(table, ...)
width <- grobWidth(g)
height <- grobHeight(g)
g <- editGrob(g, vp=viewport(x = x, y = y, width=width,
height=height, just = just))
ggname(.$my_name(), g)
})
}

required_aes <- c("x", "y")
default_aes <- function(.)
aes(x=0.5, y=0.5)
default_stat <- function(.) StatIdentity
guide_geom <- function(.) "table"

draw_legend <- function(., data, ...) { }


icon <- function(.) { }

examples <- function() {#

require(gridExtra)
require(ggExtra)

dsmall <- diamonds[sample(nrow(diamonds), 100), ]

d <- ggplot(dsmall, aes(carat, price)) +
annotate("table", x=1, y=10000, table=head(iris[, 1:3], 3),
just=c("right","top")) +
geom_point()
d

}
})

geom_table <- GeomTable$build_accessor()

Felipe Carrillo

unread,
Jul 11, 2010, 5:51:49 PM7/11/10
to baptiste auguie, ggplot2
Thanks Baptiste, if if update the package, the updated function will be
available?

baptiste auguie

unread,
Jul 12, 2010, 3:50:36 AM7/12/10
to Felipe Carrillo, ggplot2
If you take the package from googlecode, yes, otherwise wait a day or
two for r-forge binaries.

baptiste

On Sun, Jul 11, 2010 at 11:51 PM, Felipe Carrillo

Reply all
Reply to author
Forward
0 new messages