首页 文章

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

提问于
浏览
63

我想生成一个具有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)")

哪个生成
enter image description here

大多数这些面板看起来足以让我在报告中包含 . 但是,需要改进显示自相关的图 . 使用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

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

3 回答

  • 49

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

  • 13

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

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

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

    在这里它看起来是最终的解决方案:

    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
    
  • 5

    您可以将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)
    

相关问题