グリッドアレンジについて質問したところ、素晴らしい回答がありました。プロット間のスペースを減らしたいのですが、エラーが発生します。最初に動作するコードを示し、次にエラー コード (私が試したこと) を示します。私は実際にそれを見つけることができずgrid.arrange
、常にそれが由来であると想定していましgridExtra
たが、間違っている可能性があります.
だから2つの部分:
- グリッド配置でプロット間のスペースを減らすにはどうすればよいですか
- どこでドキュメントを見つけることができますか
grid.arrange
(Baptiste、あなたが gridExtra を維持していることは知っているので、意図したとおりに使用していない場合は、パッケージの考え方や使用法を修正してください。)
良いコード悪いスペース
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
coord_flip() + ylab("")
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip()
gA <- ggplot_gtable(ggplot_build(A))
gB <- ggplot_gtable(ggplot_build(B))
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
悪いコード(私の試み)
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
coord_flip() + ylab("") + theme(plot.margin= unit(1, "cm"))
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip()
gA <- ggplot_gtable(ggplot_build(A))
gB <- ggplot_gtable(ggplot_build(B))
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
エラー:
Error in `[.unit`(theme$plot.margin, 2) :
Index out of bounds (unit subsetting)