(Offtopic?) gtable produces grid background!? How to remove it?

78 views
Skip to first unread message

Johannes Graumann

unread,
May 26, 2013, 9:00:49 AM5/26/13
to ggp...@googlegroups.com
Hello,

When producing a gtable as desccribed here https://groups.google.com/d/msg/ggplot2/PfT5K88OPpI/d9VKszTiouUJ , and proceed to call "plot()" on the result, the output plot has a grid of lines plotted in the background!? Where does that come from? Can one switch that functionality off?

As always: thank you for your time and any hint you might be able to give.

Joh

Johannes Graumann

unread,
May 26, 2013, 1:23:02 PM5/26/13
to ggp...@googlegroups.com, Martín Bel, baptiste auguie
By popular demand: a more complete problem demonstration:

library(ggplot2)
library(gtable)

# Layout the gtable
# Prepare for the addition of inter-column/row space of 2.5 mm
tmpGTable <- gtable(
  widths=unit(x=rep(30,2),units="mm"),
  heights=unit(x=rep(30,3),units="mm"),
  name="Figure1")
# Add inter-column/raw space
tmpGTable <- gtable_add_col_space(tmpGTable,unit(2.5,units="mm"))
tmpGTable <- gtable_add_row_space(tmpGTable,unit(2.5,units="mm"))
gtable_show_layout(tmpGTable)

tmpB <- ggplot(data=diamonds, aes(carat, price)) +
  xlim(0, 2) +
  geom_point()
tmpB <- tmpB + theme(plot.margin = unit(c(0,0,0,0), "mm"),plot.background=element_rect(fill="transparent"))
tmpGTable <- gtable_add_grob(x=tmpGTable,grobs=ggplotGrob(tmpB),t=1,l=3)
plot(tmpGTable)
(tmpDir <- tempdir())
png(filename=file.path(tmpDir,"test.png"),width=62.5,height=95,units="mm",res=200)
plot(tmpGTable)
dev.off()

The result on my system is attached. Note the faint grid in the background of the whole plot not the plot.background of the ggplot in the uper right - I'm perfectly fine with that.

My sessionInfo() is:
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8      
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8  
 [7] LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C             
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C      

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] gtable_0.1.2    ggplot2_0.9.3.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       labeling_0.1       MASS_7.3-26      
 [6] munsell_0.4        plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2   
[11] scales_0.2.3       stringr_0.6.2      tools_3.0.1

Thanks, Joh
test.png

baptiste auguie

unread,
May 26, 2013, 5:54:07 PM5/26/13
to Johannes Graumann, ggp...@googlegroups.com, Martín Bel
Have a look at gtable:::plot.gtable ...

:)

I don't now why plot is defined this way, but you probably want to use print or grid.draw.

HTH,

baptiste

Johannes Graumann

unread,
May 27, 2013, 5:33:49 AM5/27/13
to ggp...@googlegroups.com, Johannes Graumann
Thanks for hunting this down! Stupid me.

"print" in the case of a gtable just prints a structural overview to the console, but "grid.draw" is doing it nicely.

Thank you again.

Joh
Reply all
Reply to author
Forward
0 new messages