ここに1つの可能な出発点があります。適切な伝説を持つ2つの異なるプロット、すなわち「明るい」と「薄い」を作成します。凡例をプロットオブジェクトから抽出します。次に、grid
viewport
をプロット用に1つ、凡例ごとに1つずつ使用して、ピースをまとめます。
library(grid)
library(gtable)
# create plot with legend with alpha = 1
g1 <- ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) +
geom_bar(stat = "identity", position = "dodge") +
scale_alpha_discrete(range = c(0.5, 1)) +
theme_bw() +
guides(fill = guide_legend(title = "A",
title.hjust = 0.4),
alpha = FALSE) +
theme_bw() +
theme(legend.text = element_blank())
g1
# grab legend
legend_g1 <- gtable_filter(ggplot_gtable(ggplot_build(g1)), "guide-box")
# create plot with 'pale' legend
g2 <- ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) +
geom_bar(stat = "identity", position = "dodge") +
scale_alpha_discrete(range = c(0.5, 1)) +
guides(fill = guide_legend(override.aes = list(alpha = 0.5),
title = "B",
title.hjust = 0.3),
alpha = FALSE) +
theme_bw()
g2
# grab legend
legend_g2 <- gtable_filter(ggplot_gtable(ggplot_build(g2)), "guide-box")
# arrange plot and legends
# legends to the right
# define plotting regions (viewports)
vp_plot <- viewport(x = 0.4, y = 0.5,
width = 0.8, height = 1)
vp_legend_g1 <- viewport(x = 0.85, y = 0.5,
width = 0.4, height = 0.4)
vp_legend_g2 <- viewport(x = 0.90, y = 0.5,
width = 0.4, height = 0.4)
# clear current device
grid.newpage()
# add objects to the viewports
# plot without legend
print(g1 + theme(legend.position = "none"), vp = vp_plot)
upViewport(0)
pushViewport(vp_legend_g1)
grid.draw(legend_g1)
upViewport(0)
pushViewport(vp_legend_g2)
grid.draw(legend_g2)
# legends on top
vp_plot <- viewport(x = 0.5, y = 0.4,
width = 1, height = 0.85)
vp_legend_g1 <- viewport(x = 0.5, y = 0.9,
width = 0.4, height = 0.4)
vp_legend_g2 <- viewport(x = 0.55, y = 0.9,
width = 0.4, height = 0.4)
grid.newpage()
print(g1 + theme(legend.position = "none"), vp = vp_plot)
upViewport(0)
pushViewport(vp_legend_g1)
grid.draw(legend_g1)
upViewport(0)
pushViewport(vp_legend_g2)
grid.draw(legend_g2)
2つだけを使用するように最速の解決策は次のようになります**グリッド**ビューポートを使用してプロットとその凡例に別々の領域を割り当て、次に** gridBase **パッケージを使用して手作業の凡例を上部ビューポートに配置します。 ( 'vignette(" gridBase ")'はイントロを与える、または '[r] gridBase'をここで検索してください。) –
@ JoshO'Brien' gridBase'については知りませんでした。 – Gregor
ええ、それは時には非常に便利です。 [Here](http://stackoverflow.com/questions/11489447/combining-two-plots-in-r/11496362#11496362)と[here](http://stackoverflow.com/questions/9985013/how-do -you-draw-a-line-across-a-multiple-figure-environment-in-r/9985936#9985936)は、それ以外のトリッキーな効果を達成するために私が使用した場所です。 –