Reputation: 4274
I am trying to plot multiple plots in one page. I know functions like gridExtra::grid.arrange
that can plot graphs generated by ggplot2
package. The problem I am facing is that I have two plots (bar.plot
and density.plot
below) that are generated by ggplot2
package and one plot generated using limma::vennDiagram
function. I have tried the below but it is not working:
output <- paste('summary.pdf')
pdf(output,width = 25,height = 20)
par(mfrow = c(3, 3))
plot(bar.plot)
plot(density.plot)
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)))
invisible(dev.off())
dat.venn is a data of type VennCounts:
I-H-10003-T1-D1 I-H-10003-T2-D1 I-H-10003-T3-D1 Counts
0 0 0 0
0 0 1 41
0 1 0 81
0 1 1 66
1 0 0 10
1 0 1 2
1 1 0 4
1 1 1 56
attr(,"class")
[1] "VennCounts"
I am unable to find a venn diagram package that is compatible with the grid.arrange
function. I don't think that VennCounts
cant be printed out with grid.arrange
function and ggplot2
plots can be printed out with par
function.
UPDATE: I tried using pushViewport but it is still printing the venn diagram on the next page:
pdf(output,width = 25,height = 20)
# Create layout : nrow = 2, ncol = 2
pushViewport(viewport(layout = grid.layout(2, 2)))
# A helper function to define a region on the layout
define_region <- function(row, col){
viewport(layout.pos.row = row, layout.pos.col = col)
}
# Arrange the plots
print(bar.plot, vp = define_region(1, 1:2))
print(density.plot, vp = define_region(2, 1))
print(vennDiagram(dat.venn, circle.col = col,cex = c(3,3,3)), vp = define_region(2, 2))
dev.off()
Any help would be much appreciated!
Upvotes: 3
Views: 4568
Reputation: 32789
The gridGraphics
package, like gridBase
, makes the mixing of grid, ggplot, lattice and base graphics fairly straightforward. The grid.echo()
function converts a base graphic into a grid graphic. (The advantage of using gridGraphics
is that base graphics become amenable to editing and modification via grid
; see gridGraphics.pdf.)
In the following, I draw two ggplots and a venn diagram (taken for the vennDiagram
help page in the limma
package), and I position the three plots within viewports, making sure the venn diagram plot is subjected to the grid.echo
treatment.
UPDATE to ggplot2
2.0.0 (Default stat
for geom_bar
is stat = "count"
)
library(ggplot2)
library(gridGraphics)
library(grid)
library(limma) # From bioC software repository
grid.newpage()
# Position 1st ggplot
pushViewport(viewport(y = 1, height = 1/3, just = "top"))
gg1 <- ggplot(mtcars, aes(x = mpg)) + geom_density()
print(gg1, newpage = FALSE)
upViewport()
# Position 2nd ggplot
pushViewport(viewport(y = .5, height = 1/3, just = "centre"))
gg2 <- ggplot(mtcars, aes(x = factor(carb, levels = 1:8))) +
geom_bar() + scale_x_discrete(drop = FALSE)
print(gg2, newpage = FALSE)
upViewport()
# Function to draw venn diagram - the venn diagram is taken from ?vennDiagram
plotfun <- function() {
Y <- matrix(rnorm(100*6),100,6)
Y[1:10,3:4] <- Y[1:10,3:4]+3
Y[1:20,5:6] <- Y[1:20,5:6]+3
design <- cbind(1,c(0,0,1,1,0,0),c(0,0,0,0,1,1))
fit <- eBayes(lmFit(Y,design))
results <- decideTests(fit)
a <- vennCounts(results)
print(a)
mfrow.old <- par()$mfrow
par(mfrow=c(1,2))
vennDiagram(a)
vennDiagram(results,
include=c("up", "down"),
counts.col=c("red", "blue"),
circle.col = c("red", "blue", "green3"))
}
# Position the venn diagram
pushViewport(viewport(y = 0, height = 1/3, just = "bottom"))
grid.echo(plotfun, newpage = FALSE)
upViewport(0)
Upvotes: 2
Reputation: 7664
First, save each of the files as .png files, such as save(yourfile, file = "yourname.png")
.
Second, use the read.PNG function of the grid package to load them, such as yours.png <- readPNG("yourfile.PNG")
After that, convert them with the rasterGrob function as in g1 <- rasterGrob(yours.png, interpolate=TRUE)
.
Once all the plots are comparable in format, grid.arrange()
does the trick. It might look like this:
grid.arrange(g1, g2, g3, nrow=1) # one row
dev.copy(png,'threeplots.png') # to save the array of plots
dev.off() # to close the device
Upvotes: 3