在 gtable 对象中设置宽度会折叠图;这曾经有效,但不再有效。
Setting width in gtable object collapses plot; this used to work but it doesn't anymore.
以下代码曾经有效,但现在无效了。有人知道发生了什么事吗?一定是底层 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)
结果图如下:
它应该是这样的(y 轴线完全垂直对齐):
错误似乎发生在这一行:
g.iris$widths[1:3] <- max.widths
如有任何见解,我们将不胜感激。
请注意,类似的解决方案已经使用了很长时间,例如参见here. 而 cowplot 中的 plot_grid()
函数也使用这样的代码来对齐图,它仍然有效。所以这让我完全迷惑了。
编辑
使用 grid
版本 3.3.0,这不再是问题。即下面包含grid:::unit.list()
的代码行可以删除。
问题与设置单位的方式有关。看看g.iris$widths
。您会注意到数字在那里,但单位已被删除。看到这个 , and 。将绘图转换为 gtables 后,您需要:g.iris$widths = grid:::unit.list(g.iris$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)
以下代码曾经有效,但现在无效了。有人知道发生了什么事吗?一定是底层 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)
结果图如下:
它应该是这样的(y 轴线完全垂直对齐):
错误似乎发生在这一行:
g.iris$widths[1:3] <- max.widths
如有任何见解,我们将不胜感激。
请注意,类似的解决方案已经使用了很长时间,例如参见here. 而 cowplot 中的 plot_grid()
函数也使用这样的代码来对齐图,它仍然有效。所以这让我完全迷惑了。
编辑
使用 grid
版本 3.3.0,这不再是问题。即下面包含grid:::unit.list()
的代码行可以删除。
问题与设置单位的方式有关。看看g.iris$widths
。您会注意到数字在那里,但单位已被删除。看到这个 g.iris$widths = grid:::unit.list(g.iris$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)