plots generated by 'plot' and 'ggplot' side-by-side - r

Is there a way to put the plot generated by plot function and the plot by ggplot function in R in one page side-by-side?
It is easy to put plots created by the same function into one page using par or multiplot function, but I can't figure out the above question.

You can do this using the gridBase package and viewPorts.
library(grid)
library(gridBase)
library(ggplot2)
# start new page
plot.new()
# setup layout
gl <- grid.layout(nrow=1, ncol=2)
# grid.show.layout(gl)
# setup viewports
vp.1 <- viewport(layout.pos.col=1, layout.pos.row=1)
vp.2 <- viewport(layout.pos.col=2, layout.pos.row=1)
# init layout
pushViewport(viewport(layout=gl))
# access the first position
pushViewport(vp.1)
# start new base graphics in first viewport
par(new=TRUE, fig=gridFIG())
plot(x = 1:10, y = 10:1)
# done with the first viewport
popViewport()
# move to the next viewport
pushViewport(vp.2)
ggplotted <- qplot(x=1:10,y=10:1, 'point')
# print our ggplot graphics here
print(ggplotted, newpage = FALSE)
# done with this viewport
popViewport(1)
This example is a modified version of this blog post by Dylan Beaudette

Yes. They are both grid-based graphics systems and return graphical objects. Take a look at the grid.arrange function in gridExtra package

Related

Arrange odd number of plots using rasterVis and gridExtra

I am trying to plot a panel with seven rasters using the levelplot function of the rasterVis package, combined with gridExtra's grid.arrange.
I almost get what I need by using the following code:
# load required packages
library(rasterVis)
library(gridExtra)
# load sample raster
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
# create plots
p1 <- levelplot(r, xlab=NULL, ylab=NULL, margin=FALSE)
p2 <- levelplot(r*2, xlab=NULL, ylab=NULL, margin=FALSE,colorkey=FALSE)
# put plots in list
p.list <- list(p1,p2,p2,p2,p2,p2,p2)
# create layout
lay <- rbind(c(1,1,1),
c(2,3,4),
c(5,6,7))
# arrange plots
grid.arrange(grobs=p.list, layout_matrix=lay)
which yields this figure:
However, there are some things I still need to improve:
How to decrease the blank space between the plots in the bottom rows?
How to add a single, combined legend for the six bottom rasters, preferentially put to the bottom of the figure?
Is this possible to achieve using rasterVis and gridExtra? Is there any other approach that can be used?
The white space is a combination of lattice margin settings, but also the plots having a fixed aspect ratio (they can't be too close unless the device itself has a compatible aspect ratio).
With regards the legend, you could use draw.colorkey(), but from what I can tell you need to manually ensure that the colours match by passing them explicitly to both plots and the key.
# load required packages
library(rasterVis)
library(gridExtra)
# load sample raster
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
my_theme <- rasterTheme(region = blues9)
# create plots
p1 <- levelplot(r, xlab=NULL, ylab=NULL, margin=FALSE, par.settings = my_theme)
leg <- p1$legend$right$args$key
p1$legend <- list()
p2 <- levelplot(r*2, xlab=NULL, ylab=NULL, margin=FALSE,colorkey=FALSE, par.settings = my_theme)
# put plots in list
p.list <- list(p1,p2,p2,p2,p2,p2,p2)
# create layout
lay <- rbind(c(NA,1,NA),
c(2,3,4),
c(5,6,7),
c(8,8,8))
leg$col <- my_theme$regions$col
legGrob <- draw.colorkey(key = leg, vp = grid::viewport(height=0.5))
# arrange plots
grid.arrange(grobs=c(p.list, list(legGrob)), layout_matrix=lay,
vp = grid::viewport(width=0.7,height=1))
(needless to say, facetting seems the better option by a wide margin)

Using a pheatmap in arrangeGrob

I'm attempting to plate two plots in the same .jpg using arrangeGrob().
I've only just started learning about grids and grobs and I think I know what the problem is: pheatmap is a grid object and containing grob objects, not allowing me to put it in arrangeGrob. Is this true?
Would I somehow need to put the qplot in a grid and the pheatmap in a grid and then put those grids in a new grid?
library(grid)
library(gridExtra)
library(pheatmap)
library(ggplot2)
hmdat=rbind(c(1,2,3),
c(3,4,5),
c(5,6,7))
hm=pheatmap(hmdat)
qp=qplot(1,1)
lm=rbind(c(1,2,2),
c(1,2,2))
jpeg("plots.jpg")
arrangeGrob(qp,hm, layout_matrix=lm)
dev.off()
The above code snippet runs just fine when using
arrangeGrob(qp,qp, layout_matrix=lm)
I'm not sure if you wanted to have 6 figures or you wanted to have two figures one with twice as wide as the other one (I tried to do minimum code change):
library("grid")
library("gridExtra")
library("pheatmap")
library("ggplot2")
hmdat=rbind(c(1,2,3),
c(3,4,5),
c(5,6,7))
hm <- pheatmap::pheatmap(hmdat)
qp <- qplot(1,1)
lm <- rbind(c(1,2,2),
c(1,2,2))
grid.arrange(grobs = list(qp,hm[[4]]), layout_matrix = lm)
which will give you:
The same way you can have multiple pheatmaps side-by-side:
library("grid")
library("gridExtra")
library("pheatmap")
hmdat <- rbind(c(1,2,3),
c(3,4,5),
c(5,6,7))
hm <- pheatmap::pheatmap(hmdat)
lm <- rbind(c(1,2),
c(3,3))
grid.arrange(grobs = list(hm[[4]],
hm[[4]],
hm[[4]]),
layout_matrix = lm)
As #hrbrmstr mentioned in the comment, you should use the 4th item in the pheatmap object. Also remember to provide grobs as list to the grid.arrange

Arrange base plots and grid.tables on the same page

I have 2 plots (created using Base graphics) and 2 data frames that I would like to combine onto one sheet in a PDF. I'm using grid.table to create a tableGrobs from my data frames. I'm having a lot of difficulty formatting the PDF output. In particular, I've been unsuccessful keeping all the objects on the same page. I want the right pannel to contain one graph, and the left panel to contain the other graph, and 2 tables below (landscape format).
Currently my code is something like the following:
library('gridExtra')
pdf("Rplots.pdf", paper = "USr", height = 8.5, width = 11)
layout(matrix(c(1,3, 2,3, 4,3), nrow = 3, ncol = 2, byrow = TRUE))
plot(myPlot1)
grid.table(df1)
plot(myPlot2)
grid.table(df2)
dev.off()
I do not want to use ggplot2.
To combine base plots and grid objects the package gridBase is useful.
A rough worked example base on your layout above
library(grid)
library(gridBase)
library(gridExtra)
layout(matrix(c(1,3, 2,3, 4,3), nrow = 3, ncol = 2, byrow = TRUE))
# First base plot
plot(1:10)
# second base plot
frame()
# Grid regions of current base plot (ie from frame)
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
# Table grob
grob <- tableGrob(iris[1:2,1:2])
grid.draw(grob)
popViewport(3)
# third base plot
plot(1:10)
# fourth
frame()
vps <- baseViewports()
pushViewport(vps$inner, vps$figure, vps$plot)
grid.draw(grob)
popViewport(3)
Which gives

R - save multiplot to file

I’d really appreciate your help with the following problem. I know several ways to save a single plot to a file. My question is: How do I correctly save a multiplot to a file?
To begin with, I’m not an experienced R user. I use ggplot2 to create my plots, and another thing I should probably mention is that I use the RStudio GUI. Using an example from the R Cookbook, I'm able to create multiple plots in one window.
I would like to save this so-called multiplot to a file (preferably as jpeg), but somehow fail to do this.
I’m creating the multiplot as follows:
##define multiplot function
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# Make the panel
plotCols = cols # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
## define subplots (short example here, I specified some more aesthetics in my script)
plot1a <- qplot(variable1,variable2,data=Mydataframe1)
plot1b <- qplot(variable1,variable3,data=Mydataframe1)
plot1c <- qplot(variable1,variable2,data=Mydataframe2)
plot1d <- qplot(variable1,variable3,data=Mydataframe2)
## plot in one frame
Myplot <- multiplot(plot1a,plot1b,plot1c,plot1d, cols=2)
This gives the desired result. The problem arises when I try to save to a file. I can do this manually in RStudio (using Export -> Save plot as image), but I would like to run everything in a script. I manage to save only subplot1d (which is last_plot()), and not the complete multiplot.
What I’ve tried so far:
Using ggsave
ggsave(filename = "D:/R/plots/Myplots.jpg")
This results in only subplot 1d being saved.
Using jpeg(), print() and dev.off()
jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE)
print(Myplot)
dev.off()
This results in a completely white image (just the background I assume). print(Myplot) returns NULL.
Not sure what I’m doing wrong here. My lack of understanding R is the reason I am stuck trying to find a solution. Can anyone explain what I’m doing wrong and perhaps suggest a way to solve my problem(s)?
Its because Myplot is the returned value from your multiplot function, and it returns nothing (its job is to print the graphs). You need to call multiplot with the jpeg device open:
jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE)
multiplot(plot1a,plot1b,plot1c,plot1d, cols=2)
dev.off()
should work.
Using the example code (R cookbook), it works for me
png("chickweight.png")
multiplot(p1, p2, p3, p4, cols=2)
dev.off()
And for completeness sake, ggsave does not work as it only saves the last printed ggplot object, which in your case is just the last plot. This is caused by the fact that multiplot creates the plot by drawing the ggplot objects onto different subsets of the total graphics device. An alternative is to create the plot by combining the ggplot objects into one big ggplot object, and then printing the object. This would be compatible with ggsave. This approach is implemented by arrangeGrob in the gridExtra package.

How to put two 'vcd' grid graphics in a single plot?

I would like to place two (somewhat non-standard) grid graphics in a single plot in R.
Try:
require(vcd)
mosaic(Titanic)
assoc(Titanic)
The trouble is that these aren't lattice graphics, and to my knowledge do not come with a layout argument or similar. And since these are grid graphs, they're impervious to base graph tricks like par(mfrow=c(1,2)).
How can I place the two graphs above in a single plot, with both graphs on the same line?
I already tried the suggestions in How to plot grid plots on a same page?, but they don't seem to work for vcd plots. Ultimately I would like to obtain something similar to:
Neither plot seems to return any object and I cant see how to grab the grobs from looking at grid.ls(). So using the idea from this answer
library(vcd)
library(gridGraphics)
library(gridExtra)
mosaic(Titanic)
m <- grid.grab()
assoc(Titanic)
a <- grid.grab()
grid.newpage()
grid.arrange(m, a, ncol=2)
Im sure there will be a more grid-like approach but ...
Something similar to the solution in How to plot grid plots on a same page? can also be used for vcd displays. The difference is that you need to set newpage = FALSE (to prevent opening a new display) and you need to push and pop the viewport yourself (which can be handy when re-using vcd graphics in more complicated displays such as partykit trees).
The mosaic and association display for the Titanic data can be visualized as:
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1))
mosaic(Titanic, newpage = FALSE)
popViewport()
pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
assoc(Titanic, newpage = FALSE)
popViewport()
yielding
Another option is vcd’s mplot() function (for details, see ?vcd::mplot):
library(vcd)
mplot(
mosaic(Titanic, return_grob = TRUE),
assoc(Titanic, return_grob = TRUE),
keep_aspect_ratio = FALSE
)

Resources