60

プロットしたいいくつかの異なるカテゴリがあります。これらは異なるカテゴリであり、それぞれに独自のラベル セットがありますが、ドキュメント内でグループ化することは理にかなっています。以下に、いくつかの単純な積み上げ棒グラフの例を示します。

df <- data.frame(x=c("a", "b", "c"),
                 y=c("happy", "sad", "ambivalent about life"))
ggplot(df, aes(x=factor(0), fill=x)) + geom_bar()
ggplot(df, aes(x=factor(0), fill=y)) + geom_bar()

\subfigure問題は、ラベルが異なると、凡例の幅が異なることです。つまり、プロットの幅が異なるため、テーブルや要素を作成すると見栄えが悪くなります。どうすればこれを修正できますか?

プロットまたは凡例の幅 (絶対または相対) を明示的に設定する方法はありますか?

x(広い)に基づくグラフ1 y に基づくグラフ 2 (狭い方)

4

6 に答える 6

43

編集:eggパッケージ で非常に簡単

# install.packages("egg")

library(egg)

p1 <- ggplot(data.frame(x=c("a","b","c"),
                        y=c("happy","sad","ambivalent about life")),
             aes(x=factor(0),fill=x)) + 
      geom_bar()
p2 <- ggplot(data.frame(x=c("a","b","c"),
                        y=c("happy","sad","ambivalent about life")),
             aes(x=factor(0),fill=y)) + 
      geom_bar()

ggarrange(p1,p2, ncol = 1)

ggplot2 2.2.1 に更新されたオリジナル

gtableパッケージの関数を使用し、凡例ボックスの幅に焦点を当てたソリューションを次に示します。(より一般的な解決策については、こちらを参照してください。)

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

# Your plots
p1 <- ggplot(data.frame(x=c("a","b","c"),y=c("happy","sad","ambivalent about life")),aes(x=factor(0),fill=x)) + geom_bar()
p2 <- ggplot(data.frame(x=c("a","b","c"),y=c("happy","sad","ambivalent about life")),aes(x=factor(0),fill=y)) + geom_bar()

# Get the gtables
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

# Set the widths
gA$widths <- gB$widths

# Arrange the two charts.
# The legend boxes are centered
grid.newpage()
grid.arrange(gA, gB, nrow = 2)

さらに、凡例ボックスを左揃えにする必要がある場合は、ここから@Juliusによって書かれたコードを借ります

p1 <- ggplot(data.frame(x=c("a","b","c"),y=c("happy","sad","ambivalent about life")),aes(x=factor(0),fill=x)) + geom_bar()
p2 <- ggplot(data.frame(x=c("a","b","c"),y=c("happy","sad","ambivalent about life")),aes(x=factor(0),fill=y)) + geom_bar()

# Get the widths
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

# The parts that differs in width
leg1 <- convertX(sum(with(gA$grobs[[15]], grobs[[1]]$widths)), "mm")
leg2 <- convertX(sum(with(gB$grobs[[15]], grobs[[1]]$widths)), "mm")

# Set the widths
gA$widths <- gB$widths

# Add an empty column of "abs(diff(widths)) mm" width on the right of 
# legend box for gA (the smaller legend box)
gA$grobs[[15]] <- gtable_add_cols(gA$grobs[[15]], unit(abs(diff(c(leg1, leg2))), "mm"))

# Arrange the two charts
grid.newpage()
grid.arrange(gA, gB, nrow = 2)

ここに画像の説明を入力

代替ソリューションパッケージには、グロブを 1 つのグロブに結合する関数が ありrbindます。ここのグラフでは、 を使用して幅を設定する必要がありますが、CRAN バージョンのではエラーがスローされます。cbindgtablesize = "max"gtable

1 つのオプション: 2 番目のプロットの凡例が広いことは明らかです。したがって、size = "last"オプションを使用してください。

# Get the grobs
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

# Combine the plots
g = rbind(gA, gB, size = "last")

# Draw it
grid.newpage()
grid.draw(g)

左揃えの凡例:

# Get the grobs
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

# The parts that differs in width
leg1 <- convertX(sum(with(gA$grobs[[15]], grobs[[1]]$widths)), "mm")
leg2 <- convertX(sum(with(gB$grobs[[15]], grobs[[1]]$widths)), "mm")

# Add an empty column of "abs(diff(widths)) mm" width on the right of 
# legend box for gA (the smaller legend box)
gA$grobs[[15]] <- gtable_add_cols(gA$grobs[[15]], unit(abs(diff(c(leg1, leg2))), "mm"))

# Combine the plots
g = rbind(gA, gB, size = "last")

# Draw it
grid.newpage()
grid.draw(g)

2 番目のオプションはrbind、Baptiste のgridExtraパッケージから使用することです

# Get the grobs
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

# Combine the plots
g = gridExtra::rbind.gtable(gA, gB, size = "max")

# Draw it
grid.newpage()
grid.draw(g)

左揃えの凡例:

# Get the grobs
gA <- ggplotGrob(p1)
gB <- ggplotGrob(p2)

# The parts that differs in width
leg1 <- convertX(sum(with(gA$grobs[[15]], grobs[[1]]$widths)), "mm")
leg2 <- convertX(sum(with(gB$grobs[[15]], grobs[[1]]$widths)), "mm")

# Add an empty column of "abs(diff(widths)) mm" width on the right of 
# legend box for gA (the smaller legend box)
gA$grobs[[15]] <- gtable_add_cols(gA$grobs[[15]], unit(abs(diff(c(leg1, leg2))), "mm"))

# Combine the plots
g = gridExtra::rbind.gtable(gA, gB, size = "max")

# Draw it
grid.newpage()
grid.draw(g)
于 2013-04-28T00:40:15.843 に答える
9

@hadleyが示唆するように、rbind.gtableこれを処理できるはずです。

  grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), size="last"))

ただし、レイアウトの幅は理想的にsize="max"は である必要があり、一部のタイプのグリッド ユニットにはうまく対応できません。

于 2013-04-29T15:13:39.073 に答える