我已经生成了一个结合了ggplot和基本图形的图形:
t <- c(1:(24*14))
P <- 24
A <- 10
y <- A*sin(2*pi*t/P)+20
#*****************************************************************************
par(mfrow = c(2,1))
plot(y,type = "l",xlab = "Time (hours)",ylab = "Amplitude")
aa <- par("mai")
plot.new()
require(gridBase)
vps <- baseViewports()
pushViewport(vps$figure)
pushViewport(plotViewport(margins = aa)) ## I use 'aa' to set the margins
#*******************************************************************************
require(ggplot2)
acz <- acf(y, plot = FALSE)
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()
grid.draw(ggplotGrob(p)) ## draw the figure
我使用plotViewport命令并根据第一个面板的尺寸设置面板的尺寸,通过par("mai")获得 . 附图显示了结果 .
但是,两个面板的尺寸不匹配,即第二个面板似乎比第一个面板略宽 . 如何在不必手动设置边距的情况下克服此问题
pushViewport(plotViewport(c(4,1.2,0,1.2)))
2 回答
这应该给你一些提示:
但是,真的,你为什么不在ggplot2中做所有事情?
主要思想是推动2个视口的baseviewports以获得绘图面板的尺寸 . 解决方案不是一般的 .
首先,我绘制我的基本情节
其次,我得到了情节面板的尺寸 . vpp将仅用于ggplot grobs的维度(类似于上面baptiste的想法)
ggplot2将savec绘制为grob表:
我改变了凹凸的尺寸 . (这里为什么解决方案不一般)
我情节