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

问题描述 投票:68回答:4

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

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不是基本图形,我们无法将ggplot与布局或par(mfrow)结合起来。我怎样才能用ggplot生成的自相关图替换基本图形生成的自相关图?我知道如果我的所有数据都是用ggplot制作的话我可以使用grid.arrange但是如果ggplot中只生成了一个图,我该怎么办呢?

r plot ggplot2 base biwavelet
4个回答
51
投票

使用gridBase包,只需添加2行即可。我想如果你想用网格做有趣的情节,你只需要理解和掌握视口。它实际上是网格包的基本对象。

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

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

它是如何看待最终的解决方案:

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

15
投票

您可以将print命令与grob和viewport一起使用。 首先绘制基础图形,然后添加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)

7
投票

我是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)

enter image description here


1
投票

cowplot包具有recordPlot()功能,用于捕获基础R图,以便它们可以在plot_grid()函数中组合在一起。

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)

reprex package创建于2019-03-17(v0.2.1.9000)

© www.soinside.com 2019 - 2024. All rights reserved.