編集 -R 'ggplot2'複数の(グリッドされた)プロットオブジェクトを使用して共通の唯一の凡例を配置する。
以下に含ま再現例Iは、各プロットは、例えば、凡例の1つだけのタイプを有する場合perfecty動作Add a common Legend for combined ggplotsにggplot2パッケージにthis functionを使用しています...,color =, ...
です。
しかし、私は一般的な伝説を共有する複数のプロットを手配しようとしていますが、それぞれが追加の独自の凡例などがあります。
ggplot(df1, aes(x=Site, y=RESULT, color=Position , shape=DETNAME)) +
geom_point(size=5) + ylab ("concentration (mg/L)") +
labs (shape = "Determinand")
生成:
を私は3倍にこれらを持ってし位置凡例は共有されますが、決定句と凡例は一意です。グリッド上の各プロットの上にそれらをプロットする
だから私は、私はDeterminand伝説(shape = DETNAME
)が保持されますどのgrid_arrange_shared_legend()
に渡すことができる追加の引数があるかどうかを知りたいすなわちlegend.position = "top"
のようなものを使用したが、ポジションのための共通の伝説を持ちます( color = position
)?
私は、各プロットオブジェクトに+ guides(shape = FALSE)
を追加し、私に共有位置伝説を与えるgrid_arrange_shared_legend()
を使用することができます知っているが、私はこのような何かを達成したいが、各プロットのためのユニークなDeterminandの伝説を持つ:
または、これを実行するには、grid_arrange_shared_legend()
関数のソースコードのどの部分を編集する必要があるのかを知ってもらえますか?
編集 - 再現性の例ここgrid_arrange_shared_legend()
機能を使用して
library (ggplot2)
library(gridExtra)
library (grid)
# two ggplot plot objects with multiple legends 1 common legend and 2 unique
p1<- ggplot(diamonds, aes(x=price, y= depth, color= clarity , shape= cut)) +
geom_point(size=5) + labs (shape = "unique legend", color = "common legend")
p2 <- ggplot(diamonds, aes(x=price, y= depth, color= clarity , shape= color)) +
geom_point(size=5) + labs (shape = "unique legend", color = "common legend")
# shared legend function
grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {
plots <- list(...)
position <- match.arg(position)
g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
lwidth <- sum(legend$width)
gl <- lapply(plots, function(x) x + theme(legend.position="none"))
gl <- c(gl, ncol = ncol, nrow = nrow)
combined <- switch(position,
"bottom" = arrangeGrob(do.call(arrangeGrob, gl),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight)),
"right" = arrangeGrob(do.call(arrangeGrob, gl),
legend,
ncol = 2,
widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
grid.newpage()
grid.draw(combined)
# return gtable invisibly
invisible(combined)
}
grid_arrange_shared_legend (p1,p2)
ユニークな伝説が唯一のグリッド上のプロットの1つの
質問のために正しいことを意味しユニークな凡例を保存(抽出)し、グリッド上の各プロットの上にプロットする方法ですが、共通の凡例を底は?