猿问
下载APP

请问 在R图窗口中组合基础和ggplot图形

在R图窗口中组合基础和ggplot图形

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

t <- c(1:(24*14)) P <- 24 A <- 10 y <- A*sin(2*pi*t/P)+20par(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不是基本图形,我们无法将ggplot与布局或par(mfrow)结合起来。我怎样才能用ggplot生成的自相关图替换基本图形生成的自相关图?我知道如果我的所有数据都是用ggplot制作的话我可以使用grid.arrange但是如果只有一个图是在ggplot中生成的话我该怎么办呢?


幕布斯5086720
浏览 60回答 3
3回答

慕圣8478803

使用gridBase包,只需添加2行即可。我想如果你想用网格做有趣的情节,你只需要理解和掌握视口。它实际上是网格包的基本对象。vps&nbsp;<-&nbsp;baseViewports()pushViewport(vps$figure)&nbsp;##&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;in&nbsp;the&nbsp;space&nbsp;of&nbsp;the&nbsp;autocorrelation&nbsp;plotbaseViewports()函数返回三个网格视口的列表。我在这里使用图视口对应于当前图的图形区域的视口。它是如何看待最终的解决方案:library(gridBase)par(mfrow=c(2,&nbsp;2))plot(y,type&nbsp;=&nbsp;"l",xlab&nbsp;=&nbsp;"Time&nbsp;(hours)",ylab&nbsp;=&nbsp;"Amplitude",main&nbsp;=&nbsp;"Time&nbsp;series")plot(wt.t1,&nbsp;plot.cb=FALSE,&nbsp;plot.phase=FALSE,main&nbsp;=&nbsp;"Continuous&nbsp;wavelet&nbsp;transform", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ylab&nbsp;=&nbsp;"Period&nbsp;(hours)",xlab&nbsp;=&nbsp;"Time&nbsp;(hours)")spectrum(y,method&nbsp;=&nbsp;"ar",main&nbsp;=&nbsp;"Spectral&nbsp;density&nbsp;function",&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xlab&nbsp;=&nbsp;"Frequency&nbsp;(cycles&nbsp;per&nbsp;hour)",ylab&nbsp;=&nbsp;"Spectrum")##&nbsp;the&nbsp;last&nbsp;one&nbsp;is&nbsp;the&nbsp;current&nbsp;plotplot.new()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;##&nbsp;suggested&nbsp;by&nbsp;@Joshvps&nbsp;<-&nbsp;baseViewports()pushViewport(vps$figure)&nbsp;##&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;in&nbsp;the&nbsp;space&nbsp;of&nbsp;the&nbsp;autocorrelation&nbsp;plotvp1&nbsp;<-plotViewport(c(1.8,1,0,1))&nbsp;##&nbsp;create&nbsp;new&nbsp;vp&nbsp;with&nbsp;margins,&nbsp;you&nbsp;play&nbsp;with&nbsp;this&nbsp;values&nbsp;require(ggplot2)acz&nbsp;<-&nbsp;acf(y,&nbsp;plot=F)acd&nbsp;<-&nbsp;data.frame(lag=acz$lag,&nbsp;acf=acz$acf)p&nbsp;<-&nbsp;ggplot(acd,&nbsp;aes(lag,&nbsp;acf))&nbsp;+&nbsp;geom_area(fill="grey")&nbsp;+ &nbsp;&nbsp;geom_hline(yintercept=c(0.05,&nbsp;-0.05),&nbsp;linetype="dashed")&nbsp;+ &nbsp;&nbsp;theme_bw()+labs(title=&nbsp;"Autocorrelation\n")+ &nbsp;&nbsp;##&nbsp;some&nbsp;setting&nbsp;in&nbsp;the&nbsp;title&nbsp;to&nbsp;get&nbsp;something&nbsp;near&nbsp;to&nbsp;the&nbsp;other&nbsp;plots &nbsp;&nbsp;theme(plot.title&nbsp;=&nbsp;element_text(size&nbsp;=&nbsp;rel(1.4),face&nbsp;='bold'))print(p,vp&nbsp;=&nbsp;vp1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;##&nbsp;suggested&nbsp;by&nbsp;@bpatiste

湖上湖

您可以将print命令与grob和viewport一起使用。首先绘制基础图形,然后添加ggplotlibrary(grid)#&nbsp;Let's&nbsp;say&nbsp;that&nbsp;P&nbsp;is&nbsp;your&nbsp;plotP&nbsp;<-&nbsp;ggplot(acd,&nbsp;#&nbsp;etc...&nbsp;)#&nbsp;create&nbsp;an&nbsp;apporpriate&nbsp;viewport.&nbsp;&nbsp;Modify&nbsp;the&nbsp;dimensions&nbsp;and&nbsp;coordinates&nbsp;as&nbsp;neededvp.BottomRight&nbsp;<-&nbsp;viewport(height=unit(.5,&nbsp;"npc"),&nbsp;width=unit(0.5,&nbsp;"npc"),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;just=c("left","top"),&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y=0.5,&nbsp;x=0.5)#&nbsp;plot&nbsp;your&nbsp;base&nbsp;graphics&nbsp;par(mfrow=c(2,2))plot(y,type&nbsp;#etc&nbsp;....&nbsp;)#&nbsp;plot&nbsp;the&nbsp;ggplot&nbsp;using&nbsp;the&nbsp;print&nbsp;commandprint(P,&nbsp;vp=vp.BottomRight)

慕婉清6462132

我是gridGraphics包的粉丝。出于某种原因,我遇到了gridBase的问题。library(ggplot2)library(gridGraphics)data.frame(x&nbsp;=&nbsp;2:10,&nbsp;y&nbsp;=&nbsp;12:20)&nbsp;->&nbsp;datplot(dat$x,&nbsp;dat$y)grid.echo()grid.grab()&nbsp;->&nbsp;mapgrobggplot(data&nbsp;=&nbsp;dat)&nbsp;+&nbsp;geom_point(aes(x&nbsp;=&nbsp;x,&nbsp;y&nbsp;=&nbsp;y))&nbsp;pushViewport(viewport(x&nbsp;=&nbsp;.8,&nbsp;y&nbsp;=&nbsp;.4,&nbsp;height&nbsp;=&nbsp;.2,&nbsp;width&nbsp;=&nbsp;.2))&nbsp;&nbsp;&nbsp;&nbsp;grid.draw(mapgrob)
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答