我已经产生,结合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命令,并根据所述第一面板的尺寸,由参数(“脉”)中获得设定的面板的尺寸。 附图中显示的结果。 然而,两个面板的尺寸不匹配,即,第二面板似乎是比第一稍宽。 我怎样才能克服这种无需手动设置与边距
pushViewport(plotViewport(c(4,1.2,0,1.2)))
这应该给你一些提示:
library(grid)
library(ggplot2)
require(gridBase)
par(mfrow = c(2,1))
plot(1:10)
a <- par("mai")
plot.new()
vps <- baseViewports()
pushViewport(vps$figure)
p = qplot(1:10, 1:10) + theme_bw()
g <- ggplotGrob(p)
lw = unit(a[2], "inch") - sum(g$widths[1:3])
g$widths[[2]] <- as.list(lw + g$widths[[2]])
g$widths[[4]] <- as.list(unit(1, "npc") - unit(a[2] + a[4], "inch"))
g$widths[[5]] <- unit(a[4], "inch")
grid.draw(g)
# draw a shaded vertical band to test the alignment
grid.rect(unit(a[2], "inch"), unit(0, "inch"),
unit(1,"npc") - unit(a[2] + a[4], "inch"),
unit(2,"npc"),
gp=gpar(lty=2, fill="red", alpha=0.1), hjust=0, vjust=0)
upViewport()
但是,说真的,为什么你会不会做GGPLOT2一切吗?
个主要的想法是推动2个视baseviewports来获得剧情面板的尺寸。 该解决方案是不一般。
首先,我绘制我的基本情节
t <- c(1:(24*14))
P <- 24
A <- 10
y <- A*sin(2*pi*t/P)+20
#*****************************************************************************
par(mfrow = c(2,1))
plot(t,y,type = "l",xlab = "Time (hours)",ylab = "Amplitude")
plot.new()
其次,我得到的情节面板的尺寸。 VPP将被用于只为ggplot grobs的尺寸(类似于巴普蒂斯特想法以上)
require(gridBase)
vps <- baseViewports()
vpp <- pushViewport(vps$figure,vps$plot) ## here I add a new viewport
vpp <- current.viewport()
upViewport(2)
GGPLOT2围栏哺乳动物为格罗布表:
require(ggplot2)
p <- ggplot(acd, aes(Lag, ACF)) + geom_area(fill = "grey") +
geom_hline(yintercept = c(0.05, -0.05), linetype = "dashed") +
theme_bw()
data <- ggplot_build(p)
gtable <- ggplot_gtable(data)
我改变grobs的尺寸。 (这里为什么解决方案是不一般)
gtable$heights[[2]] <- vpp$height
gtable$heights[[4]] <- vpp$height
gtable$widths[[4]] <- vpp$width
我阴谋
grid.draw(gtable)