Combine basic graphics and ggplot graphics in the figure window R

I would like to create a shape that has a combination of basic and graphic ggplot graphics. The following code shows my figure using the basic R plotting functions:

t <- c(1:(24*14)) P <- 24 A <- 10 y <- A*sin(2*pi*t/P)+20 par(mfrow=c(2,2)) plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude",main = "Time series") acf(y,main = "Autocorrelation",xlab = "Lag (hours)", ylab = "ACF") spectrum(y,method = "ar",main = "Spectral density function", xlab = "Frequency (cycles per hour)",ylab = "Spectrum") require(biwavelet) t1 <- cbind(t, y) wt.t1=wt(t1) plot(wt.t1, plot.cb=FALSE, plot.phase=FALSE,main = "Continuous wavelet transform", ylab = "Period (hours)",xlab = "Time (hours)") 

What generates enter image description here

Most of these panels look good enough to be included in my report. However, a graph showing autocorrelation needs to be improved. This looks a lot better using ggplot:

 require(ggplot2) acz <- acf(y, plot=F) acd <- data.frame(lag=acz$lag, acf=acz$acf) ggplot(acd, aes(lag, acf)) + geom_area(fill="grey") + geom_hline(yintercept=c(0.05, -0.05), linetype="dashed") + theme_bw() 

enter image description here

However, since ggplot is not a basic graph, we cannot combine ggplot with a layout or par (mfrow). How can I replace the autocorrelation graph created on the base graph with the generated ggplot? I know that I can use grid.arrange if all my drawings were done with ggplot, but how to do it if only one of the graphs is generated in ggplot?

+68
r plot ggplot2 base biwavelet
Jan 02 '13 at 15:15
source share
4 answers

Using the gridBase package, you can do this by simply adding 2 rows. I think that if you want to make a funny plot with a grid, you just need to understand and master the viewports . This is really the main mesh object.

 vps <- baseViewports() pushViewport(vps$figure) ## I am in the space of the autocorrelation plot 

The baseViewports () function returns a list of three grid viewports. I am using the Viewport shape here. The viewport corresponding to the current drawing area.

Here's what the final solution looks like:

enter image description here

 library(gridBase) par(mfrow=c(2, 2)) plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude",main = "Time series") plot(wt.t1, plot.cb=FALSE, plot.phase=FALSE,main = "Continuous wavelet transform", ylab = "Period (hours)",xlab = "Time (hours)") spectrum(y,method = "ar",main = "Spectral density function", xlab = "Frequency (cycles per hour)",ylab = "Spectrum") ## the last one is the current plot plot.new() ## suggested by @Josh vps <- baseViewports() pushViewport(vps$figure) ## I am in the space of the autocorrelation plot vp1 <-plotViewport(c(1.8,1,0,1)) ## create new vp with margins, you play with this values require(ggplot2) acz <- acf(y, plot=F) acd <- data.frame(lag=acz$lag, acf=acz$acf) p <- ggplot(acd, aes(lag, acf)) + geom_area(fill="grey") + geom_hline(yintercept=c(0.05, -0.05), linetype="dashed") + theme_bw()+labs(title= "Autocorrelation\n")+ ## some setting in the title to get something near to the other plots theme(plot.title = element_text(size = rel(1.4),face ='bold')) print(p,vp = vp1) ## suggested by @bpatiste 
+51
Jan 02 '13 at 16:29
source share

You can use the print command with grob and viewport.
First draw your basic graphics, then add ggplot

 library(grid) # Let say that P is your plot P <- ggplot(acd, # etc... ) # create an apporpriate viewport. Modify the dimensions and coordinates as needed vp.BottomRight <- viewport(height=unit(.5, "npc"), width=unit(0.5, "npc"), just=c("left","top"), y=0.5, x=0.5) # plot your base graphics par(mfrow=c(2,2)) plot(y,type #etc .... ) # plot the ggplot using the print command print(P, vp=vp.BottomRight) 
+15
Jan 02 '13 at 16:44
source share

I am a fan of the gridGraphics package. For some reason, I had problems with gridBase.

 library(ggplot2) library(gridGraphics) data.frame(x = 2:10, y = 12:20) -> dat plot(dat$x, dat$y) grid.echo() grid.grab() -> mapgrob ggplot(data = dat) + geom_point(aes(x = x, y = y)) pushViewport(viewport(x = .8, y = .4, height = .2, width = .2)) grid.draw(mapgrob) 

enter image description here

+7
Sep 28 '15 at 21:21
source share

The cowplot package has recordPlot() to capture basic R graphs so that they can be combined into plot_grid() functions.

 library(biwavelet) library(ggplot2) library(cowplot) t <- c(1:(24*14)) P <- 24 A <- 10 y <- A*sin(2*pi*t/P)+20 plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude",main = "Time series") ### record the previous plot p1 <- recordPlot() spectrum(y,method = "ar",main = "Spectral density function", xlab = "Frequency (cycles per hour)",ylab = "Spectrum") p2 <- recordPlot() t1 <- cbind(t, y) wt.t1=wt(t1) plot(wt.t1, plot.cb=FALSE, plot.phase=FALSE,main = "Continuous wavelet transform", ylab = "Period (hours)",xlab = "Time (hours)") p3 <- recordPlot() acz <- acf(y, plot=F) acd <- data.frame(lag=acz$lag, acf=acz$acf) p4 <- ggplot(acd, aes(lag, acf)) + geom_area(fill="grey") + geom_hline(yintercept=c(0.05, -0.05), linetype="dashed") + theme_bw() ### combine all plots together plot_grid(p1, p4, p2, p3, labels = 'AUTO', hjust = 0, vjust = 1) 

Created 2019-03-17 by view package (v0.2.1.9000)

+1
Mar 17 '19 at 7:06
source share



All Articles