组合ggplot和基础图形时,使面板具有相同的边距 [英] make panels with same margins when combining ggplot and base graphics
问题描述
我已经生成了一个结合了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 =时间(小时),ylab =振幅)
aa < - par(mai)
plot.new()
require (gridBase)
vps< - baseViewports()
pushViewport(vps $ figure)
pushViewport(plotViewport(margin = aa))##我使用'aa'设置边距
#*********************************************** ********************************
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 =gray)+
geom_hline(yintercept = c(0.05,-0.05),linetype =dashed)+
theme_bw()
grid.draw(ggplotGrob(p) )##绘制数字
我使用plotViewport命令并根据par(mai)获得的第一个面板的尺寸设置面板的尺寸。附图显示了结果。
但是,两个面板的尺寸不匹配,即第二个小组似乎比第一个小一些。我怎样才能克服这一点,而无需手动设置边距
pushViewport(plotViewport(c(4,1.2,0,1.2) )))
这应该给你一些提示: / p>
$ p $
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 =单位(a [2],英寸) - 和(g $ widths [1:3])
g $ widths [[2]] < - as.list(lw + g $ (单位(1,npc) - 单位(a [2] + a [4],英寸)宽度[[2]])
g $ widths [[4]] < )
g $ widths [[5]]< - 单位(a [4],英寸)
grid.draw(g)
#绘制阴影垂直带测试对齐
grid.rect(unit(a [2],inch),unit(0,inc单位(a [2] + a [4],英寸),
单位(2,npc),
gp = gpar(lty = 2,fill =red,alpha = 0.1),hjust = 0,vjust = 0)
upViewport()
但是,真的,为什么你不会在ggplot2中做所有事情?
I have generated a figure that combines ggplot and base graphics:
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
I use the plotViewport command and set the dimensions of the panel according to the dimensions of the first panel, obtained by par("mai"). The figure attached shows the outcome. However, the dimensions of both panels do not match, i.e. the second panel seems to be slightly wider than the first. How can I overcome this without having to manually set the margins with
pushViewport(plotViewport(c(4,1.2,0,1.2)))
This should give you some hints:
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()
but, really, why would you not do everything in ggplot2?
这篇关于组合ggplot和基础图形时,使面板具有相同的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!