再現可能な例は、このチュートリアルのcowplotパッケージに含まれています。共用凡例をプロットグリッドの中心に合わせる(カウプロット付き)
https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html
コピーのコード例:
library(ggplot2)
library(cowplot)
#down-sampled diamonds data set
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
# Make three plots.
# We set left and right margins to 0 to remove unnecessary spacing in the
# final plot arrangement.
p1 <- qplot(carat, price, data=dsamp, colour=clarity) +
theme(plot.margin = unit(c(6,0,6,0), "pt"))
p2 <- qplot(depth, price, data=dsamp, colour=clarity) +
theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("")
p3 <- qplot(color, price, data=dsamp, colour=clarity) +
theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("")
# arrange the three plots in a single row
prow <- plot_grid(p1 + theme(legend.position="none"),
p2 + theme(legend.position="none"),
p3 + theme(legend.position="none"),
align = 'vh',
labels = c("A", "B", "C"),
hjust = -1,
nrow = 1
)
legend_b <- get_legend(p1 + theme(legend.position="bottom"))
# add the legend underneath the row we made earlier. Give it 10% of the height
# of one plot (via rel_heights).
p <- plot_grid(prow, legend_b, ncol = 1, rel_heights = c(1, .2))
p
この例では、凡例は、グリッドの左下に位置合わせ描かれたプロットを示します。 しかし、以前は凡例がプロットの下部中央に揃えられていたため、以前とは異なっていました。ここ数ヶ月前に私の個人コードによって生成された例があります。 (また、チュートリアル、上から第三のプロットに示すように)のいずれかのパッケージの変化の未知の量が下側に整列凡例を提供した後、私の古いコードを再実行 https://s1.postimg.org/8pf2en1zen/Untitled.png(アップロードツールは、現在、私のために働いていない)
左: https://s1.postimg.org/3agjw7n9gf/Untitled2.png
質問は、凡例を下中央に揃えて描画するようにコードを調整する方法です。
はありがとうございます。これには、他の凡例のサイズに対して「試行錯誤」が必要です。 – nouse
'legend_b < - get_legend(p1 + theme(legend.position =" none "、legend.direction =" horizontal "))' これは私にとっても役に立ちます。 –
get_legend(p1 +テーマ(legend.position = "none"、legend.direction = "horizontal"))のエラー:プロットには凡例が含まれていなければなりません – nouse