11

グリッドアレンジについて質問したところ、素晴らしい回答がありました。プロット間のスペースを減らしたいのですが、エラーが発生します。最初に動作するコードを示し、次にエラー コード (私が試したこと) を示します。私は実際にそれを見つけることができずgrid.arrange、常にそれが由来であると想定していましgridExtraたが、間違っている可能性があります.

だから2つの部分:

  1. グリッド配置でプロット間のスペースを減らすにはどうすればよいですか
  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)
4

2 に答える 2

12

私はggplotを誤解していました:

require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
    coord_flip() + ylab("") + theme(plot.margin= unit(c(1, 1, -1, 1), "lines"))
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() + 
    theme(plot.margin= unit(rep(.5, 4), "lines"))


 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)
于 2012-11-08T23:20:52.710 に答える