を持つ単一の行に複数のプロット私はこのような二つのグラフの組み合わせプロット+彼らの伝説たい:私は幅を推測したくないしかしggplot2:単一伝説
library(ggplot2)
library(grid)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(price, carat, data=dsamp, colour=clarity)
p2 <- qplot(price, depth, data=dsamp, colour=clarity)
g <- ggplotGrob(p1 + theme(legend.position="bottom"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
grid.arrange(arrangeGrob(p1+theme(legend.position="right"),p2+theme(legend.position="none"),legend,ncol=3,widths=c(3/7,3/7,1/7)))
を(そしてncol
と指定して)、p1
とp2
as shown hereから抽出してください。
だから、私はこのような何か(リンクから適応コード)必要があるだろう期待:
grid_arrange_shared_legend_row <- function(...) {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position="right"))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lwidth <- sum(legend$width)
grid.arrange(
do.call(arrangeGrob, lapply(plots, function(x)
x + theme(legend.position="none"))),
legend,
ncol = length(plots)+1,
widths = unit.c(rep(unit(1, "npc") - lwidth, length(plots)), lwidth))
}
grid_arrange_shared_legend_row(p1, p2)
が、これは1行に2つのプロットではなく、1列配置されていません。
をこの質問は同様のto this one hereですが、私は適応された幅も求めています。私はその質問+回答とgithubの両方からコード抽出を使用しています。
ありがとう、これは実際に素晴らしいです。おそらく、同じコードで他の質問にも答えるべきでしょうか?それは私のGoogleの結果をかなり高くして、将来のユーザーのための素晴らしいリファレンスになります! – mts