在R图窗口中结合base和ggplotgraphics

我想生成一个具有基本和ggplotgraphics组合的graphics。 以下代码显示了使用R的基本绘图函数的graphics:

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)") 

其中产生 在这里输入图像描述

这些小组中的大部分都足以让我将其纳入我的报告。 但是,显示自相关的情节需要改进。 这看起来好多了,使用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() 

在这里输入图像描述

但是,看到ggplot不是基本graphics,我们不能将ggplot与布局或par(mfrow)结合使用。 我怎样才能取代由ggplot生成的基本graphics生成的自相关图? 我知道我可以使用grid.arrange,如果我所有的数字都是用ggplot制作的,但是如果只有其中一个绘图是在ggplot中生成的,我该怎么做?

使用gridBase包,只需要添加2行即可。 我想如果你想用网格来做一些有趣的绘图,你只需要理解和掌握视口 。 这真的是网格包的基本对象。

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

baseViewports()函数返回三个网格视口的列表。 我在这里使用graphics视口对应于当前graphics的graphics区域的视口。

这里看起来是最终的解决scheme:

在这里输入图像描述

 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 

您可以使用grob和视口的打印命令。
首先绘制你的基础graphics,然后添加ggplot

 library(grid) # Let's 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) 

我是gridGraphics软件包的粉丝。 出于某种原因,我遇到了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) 

在这里输入图像描述

Interesting Posts