2017-10-19 13 views
0

編集 -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") 

生成:

enter image description here

を私は3倍にこれらを持ってし位置凡例は共有されますが、決定句と凡例は一意です。グリッド上の各プロットの上にそれらをプロットする

だから私は、私はDeterminand伝説(shape = DETNAME)が保持されますどのgrid_arrange_shared_legend()に渡すことができる追加の引数があるかどうかを知りたいすなわちlegend.position = "top"のようなものを使用したが、ポジションのための共通の伝説を持ちます( color = position)?

私は、各プロットオブジェクトに+ guides(shape = FALSE)を追加し、私に共有位置伝説を与えるgrid_arrange_shared_legend()を使用することができます知っているが、私はこのような何かを達成したいが、各プロットのためのユニークなDeterminandの伝説を持つ: enter image description here

または、これを実行するには、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) 

enter image description here

ユニークな伝説が唯一のグリッド上のプロットの1つの

質問のために正しいことを意味しユニークな凡例を保存(抽出)し、グリッド上の各プロットの上にプロットする方法ですが、共通の凡例を底は?

答えて

4

ここでcowplotを使用することをお勧めします。

library(ggplot2) 

#reduce the number of points to plot 
diamonds2 <- diamonds[sample(nrow(diamonds), 500), ] 

p1<- ggplot(diamonds2, aes(x=price, y= depth, color= clarity , shape= cut)) + 
    geom_point(size=5) + labs (shape = "unique legend", color = "common legend") + 
    theme(legend.position = "top") 

p2 <- ggplot(diamonds2, aes(x=price, y= depth, color= clarity , shape= color)) + 
    geom_point(size=5) + labs (shape = "unique legend", color = "common legend") + 
    theme(legend.position = "top") 

cowplot::plot_grid(
    cowplot::plot_grid(
    p1 + scale_color_discrete(guide = FALSE), 
    p2 + scale_color_discrete(guide = FALSE), 
    align = 'h' 
), 
    cowplot::get_legend(p1 + scale_shape(guide = FALSE) + theme(legend.position = "bottom")), 
    nrow = 2, rel_heights = c(4, 1) 
) 

enter image description here

:このケースでは、 get_legendと凡例を取得、その後、2 plot_grid通話を組み合わせることが最も簡単です
関連する問題