在 R 图形窗口中组合 base 和 ggplot 图形 [英] Combine base and ggplot graphics in R figure window
问题描述
我想生成一个结合了 base 和 ggplot 图形的图形.以下代码使用 R 的基本绘图函数显示了我的图形:
I would like to generate a figure that has a combination of base and ggplot graphics. The following code shows my figure using the base plotting functions of 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 这看起来好多了:
Most of these panels look sufficient for me to include in my report. However, the plot showing the autocorrelation needs to be improved. This looks much better by 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()
然而,由于 ggplot 不是基础图形,我们不能将 ggplot 与布局或 par(mfrow) 结合起来.我怎样才能用 ggplot 生成的自相关图替换从基础图形生成的自相关图?我知道如果我的所有图形都是用 ggplot 制作的,我可以使用 grid.arrange 但是如果在 ggplot 中只生成一个图,我该怎么做?
However, seeing as ggplot is not a base graphic, we cannot combine ggplot with layout or par(mfrow). How could I replace the autocorrelation plot generated from the base graphics with the one generated by ggplot? I know I can use grid.arrange if all of my figures were made with ggplot but how do I do this if only one of the plots are generated in ggplot?
推荐答案
使用 gridBase 包,只需添加 2 行即可完成.我认为如果你想用网格做有趣的情节,你只需要了解和掌握视口.确实是grid包的基础对象.
Using gridBase package, you can do it just by adding 2 lines. I think if you want to do funny plot with the grid you need just to understand and master viewports. It is really the basic object of the grid package.
vps <- baseViewports()
pushViewport(vps$figure) ## I am in the space of the autocorrelation plot
baseViewports() 函数返回三个网格视口的列表.我在这里使用图视口对应于当前图的图形区域的视口.
The baseViewports() function returns a list of three grid viewports. I use here figure Viewport A viewport corresponding to the figure region of the current plot.
这是最终解决方案的样子:
Here how it looks the final solution:
library(gridBase)
library(grid)
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
")+
## 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
这篇关于在 R 图形窗口中组合 base 和 ggplot 图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!