[Tikzdevice-bugs] TikzDevice and ggplot2 panel plots -- Solved

Lorenzo Isella lorenzo.isella at gmail.com
Sun Jun 13 19:53:30 CEST 2010


Apologies, I think I figured out how to solve this myself.
See the script below.
Cheers

Lorenzo


library(ggplot2)
library(tikzDevice)

vplayout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)


draw4 <- function(pdfname, a,b,c,d,w,h) {

    pdf(pdfname,width=w, height=h)
    grid.newpage()
    pushViewport(viewport(layout=grid.layout(2,2) ) )

        print(a, vp=vplayout(1,1))
        print(b, vp=vplayout(1,2))
        print(c, vp=vplayout(2,1))
        print(d, vp=vplayout(2,2))


    dev.off()
} 



draw4tik <- function(pdfname, a,b,c,d,w,h) {

    ## pdf(pdfname,width=w, height=h)

    tikz(pdfname, standAlone = TRUE, width=w,height=h)
    grid.newpage()
    pushViewport(viewport(layout=grid.layout(2,2) ) )

        print(a, vp=vplayout(1,1))
        print(b, vp=vplayout(1,2))
        print(c, vp=vplayout(2,1))
        print(d, vp=vplayout(2,2))


    dev.off()
    tools::texi2dvi(pdfname,pdf=T)
} 



## tikz('path_comparison_tik.tex', standAlone = TRUE, width=5,height=5)
## print(gpl2)
## dev.off() 

## tools::texi2dvi('path_comparison_tik.tex',pdf=T)


data(diamonds)


set.seed(1234)

randind <- sample(nrow(diamonds),1000,replace=FALSE)
dsmall <- diamonds[randind,]

a <-  qplot(carat, data=dsmall, geom="histogram",binwidth=1)
b <-  qplot(carat, data=dsmall, geom="histogram",binwidth=.1)
c <- qplot(carat, data=dsmall, geom="histogram",binwidth=.01)
d <- qplot(carat, data=dsmall, geom="histogram",binwidth=2)


width <- 7
height <- 7

draw4( "test-4.pdf", a,b,c,d, width, height) 

draw4tik( "test-4tik.tex", a,b,c,d, width, height) 









Dear All,
I hope this is not too off topic, as I am not reporting a bug but
strictly speaking I am asking how to use tikzDevice+ggplot2 to produce
panel plots.
Consider the small snippet at the end of the email.
I use it quite often to generate panel plots without resorting to
ggplot2 faceting feature. 
Can I combine it with tikzDevice(i.e. I would like to use tikzDevice to
e.g. generate each individual plot or to use it to render the final 2 by
2 panel plot).
Any suggestion is appreciated.
Cheers

Lorenzo



##########################################################
##########################################################
##########################################################

library(ggplot2)

vplayout <- function(x, y) viewport(layout.pos.row=x, layout.pos.col=y)


draw4 <- function(pdfname, a,b,c,d,w,h) {

    pdf(pdfname,width=w, height=h)
    grid.newpage()
    pushViewport(viewport(layout=grid.layout(2,2) ) )

        print(a, vp=vplayout(1,1))
        print(b, vp=vplayout(1,2))
        print(c, vp=vplayout(2,1))
        print(d, vp=vplayout(2,2))


    dev.off()
} 

data(diamonds)


set.seed(1234)

randind <- sample(nrow(diamonds),1000,replace=FALSE)
dsmall <- diamonds[randind,]

a <-  qplot(carat, data=dsmall, geom="histogram",binwidth=1)
b <-  qplot(carat, data=dsmall, geom="histogram",binwidth=.1)
c <- qplot(carat, data=dsmall, geom="histogram",binwidth=.01)
d <- qplot(carat, data=dsmall, geom="histogram",binwidth=2)


width <- 7
height <- 7

draw4( "test-4.pdf", a,b,c,d, width, height) 







More information about the Tikzdevice-bugs mailing list