使用grid.arrange()对齐两个绘图

使用grid.arrange()对齐两个绘图,r,ggplot2,R,Ggplot2,我使用此代码创建了以下绘图: library(ggplot2) library(grid) library(gridExtra) x.test<-c(0,2,4,6,8,12,16,20,24) y=c(1,2,3,4,5,6,7,8,9) y.test=c(1,2,3,4,5,6,7,8,9) testdf<-data.frame(x.test,y.test) heat.time<-x.test heat.val<-c(0.2,0.4,0.6,0.8,0.9,

我使用此代码创建了以下绘图:

library(ggplot2)
library(grid)
library(gridExtra)

x.test<-c(0,2,4,6,8,12,16,20,24)
y=c(1,2,3,4,5,6,7,8,9)
y.test=c(1,2,3,4,5,6,7,8,9)

testdf<-data.frame(x.test,y.test) 


heat.time<-x.test
heat.val<-c(0.2,0.4,0.6,0.8,0.9,0.9,0.9,0.9,0.9)
testdf2<-data.frame(heat.time,heat.val)
plot1<-ggplot(testdf,aes(x.test,y.test))+geom_line()+
  scale_x_continuous(limits=c(0,26),breaks=c(0,2,4, 6, 8, 12,16,20,24))
plot2<-ggplot(testdf2,aes(heat.time,""))+
  geom_tile(data=testdf2,aes(fill=heat.val),height=1, width=0.7)+   
  scale_fill_gradient(low="lightblue",high="blue") +
  scale_x_continuous(limits=c(-1,26),breaks=c(-1,2,4, 6, 8, 12,16,20,24))+
  theme(
    legend.position = "top",
    axis.line=element_blank(),
    axis.text.x=element_blank(),
    axis.text.y=element_blank(),
    axis.ticks=element_blank(),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    panel.background=element_blank(),
    panel.border=element_blank(),
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    plot.background=element_blank(),
    plot.margin=unit(c(0,0,-2,0), "cm")
  )

gA <- ggplotGrob(plot2)
gB <- ggplotGrob(plot1)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, heights=c(0.25,0.75), ncol=1)   

问题似乎是因为(a)为两个图提供的
scale\u x\u continuous()
参数不相同,以及(b)限制没有在两侧对称添加空间

通过
cowplot
和对
scale\u x\u continuous()
提供的参数进行一些修改,两个图都可以按预期对齐

通过对
scale\u x\u continuous()
的修改调用

请注意,
theme\u void()
用于
plot2
,并且
plot.margin
保持不变


版本:R
3.3.2
cowplot
0.7.0,
ggplot2
2.2.1,
gridExtra
2.2.1

问题似乎是因为(a)为
scale\x\u continuous()
提供的参数对于两个绘图和(b)不相同限制并没有在两侧对称地增加空间

通过
cowplot
和对
scale\u x\u continuous()
提供的参数进行一些修改,两个图都可以按预期对齐

通过对
scale\u x\u continuous()
的修改调用

请注意,
theme\u void()
用于
plot2
,并且
plot.margin
保持不变


版本:R
3.3.2
cowplot
0.7.0,
ggplot2
2.2.1,
gridExtra
2.2.1

猜测:用
主题(plot.margin=unit(c(0,0,-2,0),“cm”)
)更改
plot 1的边距怎么样?这不起作用。可以试试cowplot软件包,
plot\grid\(g1,g2,nrow=2,align=“v”)
?使您的代码可复制,plot2 data=test缺失。只是澄清一下:R的最新版本是2016-10-31的3.3.2,您的版本3.2.3大约早了一年。因此,难怪您无法安装实际版本的
cowplot
0.7.0,该版本要求R>=3.3.0疯狂猜测:更改<代码>绘图1
带有
主题(绘图。边距=单位(c(0,0,-2,0),“cm”)
?这不起作用。可以尝试cowplot软件包,
绘图网格(g1,g2,nrow=2,align=“v”)
?使您的代码具有可复制性,plot2 data=test缺失。请澄清:R的最新版本是2016-10-31的3.3.2,您的版本3.2.3大约早了一年。因此,难怪您无法安装要求R>=3.3.0的实际版本的
cowplot
0.7.0
sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)
plot1 <- ggplot(testdf, aes(x.test, y.test)) + geom_line() +
  scale_x_continuous(limits = c(-1, 25), breaks = c(0, 2, 4, 6, 8, 12, 16, 20, 24))
plot2 <- ggplot(testdf2, aes(heat.time, "")) + 
  geom_tile(data = testdf2, aes(fill = heat.val), height = 1, width = 0.7) +
  scale_fill_gradient(low = "lightblue", high = "blue") + 
  scale_x_continuous(limits = c(-1, 25), breaks = c(0, 2, 4, 6, 8, 12, 16, 20, 24)) +
  theme_void() + theme(legend.position = "top")
cowplot::plot_grid(plot2, plot1, rel_heights=c(0.25, 0.75), ncol = 1, align = "v")