R 在gtable对象折叠图中设置宽度;这过去是可行的,但现在不行了';再也没有了。

R 在gtable对象折叠图中设置宽度;这过去是可行的,但现在不行了';再也没有了。,r,ggplot2,gtable,cowplot,R,Ggplot2,Gtable,Cowplot,下面的代码过去可以工作,但现在不行了。有人知道发生了什么事吗?它必须是基础gtable代码中的一些更改 require(cowplot) # for plot_grid() require(grid) # for unit_max() # make two plots plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + facet_grid(. ~ Species) + stat_sm

下面的代码过去可以工作,但现在不行了。有人知道发生了什么事吗?它必须是基础gtable代码中的一些更改

require(cowplot) # for plot_grid()
require(grid) # for unit_max()

# make two plots
plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + facet_grid(. ~ Species) + stat_smooth(method = "lm") +
  background_grid(major = 'y', minor = "none") + 
  panel_border()
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5)

g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

iris.widths <- g.iris$widths[1:3] # extract the first three widths, 
                                  # corresponding to left margin, y lab, and y axis
mpg.widths <- g.mpg$widths[1:3] # same for mpg plot
max.widths <- unit.pmax(iris.widths, mpg.widths) # calculate maximum widths
g.iris$widths[1:3] <- max.widths # assign max. widths to iris gtable
g.mpg$widths[1:3] <- max.widths # assign max widths to mpg gtable

plot_grid(g.iris, g.mpg, labels = "AUTO", ncol = 1)
require(cowplot)#用于绘图网格()
要求(网格)#用于单位_max()
#画两幅图
plot.iris编辑
对于
grid
3.3.0版,这不再是一个问题。也就是说,可以删除下面包含
网格:::unit.list()
的代码行

问题在于单位的设置方式。查看
g.iris$widths
。您会注意到数字在那里,但单位已被删除。看看这个,然后。将绘图转换为gtables后,需要:
g.iris$widths=grid:::unit.list(g.iris$widths)

require(网格)#用于单位_max()
要求(整流罩图)
#画两幅图

确实,
网格:::unit.list()
已经需要一段时间了;我忘了当我开始注意到ggplot2的破损时。这种未报告的网格函数几乎已经成为进行这种绘图对齐的必要条件,这在包中可能会有问题。非常感谢!轴线问题是最新ggplot2版本2.1.0中的一个缺陷。它已经在github中修复,提交给CRAN的问题尚未解决。我删除了对轴线的引用。需要澄清的是:轴线错误在ggplot2.1.0中。我已经修复了cowplot来解决这个bug,并将其提交给了CRAN。@baptiste使用最新的R开发版本,这个例子现在应该可以在没有
网格:::unit.list()的情况下工作了。
g.iris$widths[1:3] <- max.widths
require(grid) # for unit_max()
require(cowplot)

# make two plots
plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + facet_grid(. ~ Species) + stat_smooth(method = "lm") + 
  background_grid(major = 'y', minor = "none") + 
  panel_border()
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5) 

g.iris <- ggplotGrob(plot.iris) # convert to gtable   
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

g.iris$widths = grid:::unit.list(g.iris$widths)   
g.mpg$widths =  grid:::unit.list(g.mpg$widths)

iris.widths <- g.iris$widths[1:3] # extract the first three widths, 
                                  # corresponding to left margin, y lab, and y axis
mpg.widths <- g.mpg$widths[1:3] # same for mpg plot

max.widths <- unit.pmax(iris.widths, mpg.widths) # calculate maximum widths

g.iris$widths[1:3] <- max.widths # assign max. widths to iris gtable
g.mpg$widths[1:3] <- max.widths # assign max widths to mpg gtable

plot_grid(g.iris, g.mpg, labels = "AUTO", ncol = 1)