2016-05-19 10 views
2

()(A-Dのプロットにラベルを付けるため)のグリッドを作成しました。データはその(4つのわずかに異なる割合で異なるテーブルが、同じ原理)のように見えますCowplotグリッドの共有凡例をR

pfour<-ggplot(four, aes(x=Concentration, y=Percentage, fill=Phenotype)) + 
geom_bar(stat='identity',color='black') + 
scale_fill_grey(start = .4, end = .9) + 
theme_bw()+ylab("Distribution") + 
xlab("Contentration [mg/ml]") + 
ggtitle("96 hpf") + 
theme(legend.title = element_text(colour="black", size=10, face="bold")) + 
theme(legend.background = element_rect(fill="white", 
             size=0.5, linetype="solid", 
             colour ="black")) + 
scale_x_discrete(limits=c('uninjected','control','0.002', '0.02', '0.2'), 
        labels=c('uninjected\n(n=251)', 
          'control\n(n=248)', 
          '0.002\n(n=205)', 
          '0.02\n(n=222)', 
          '0.2\n(n=203)')) 

Concentration,Percentage,Phenotype 
uninjected,0.996015936,0 
uninjected,0,1 
uninjected,0.003984064,2 
uninjected,0,3 
uninjected,0,4 
control,0.995967742,0 
control,0.004032258,1 
control,0,2 
control,0,3 
control,0,4 
0.002,0.985365854,0 
0.002,0.004878049,1 
0.002,0.004878049,2 
0.002,0,3 
0.002,0.004878049,4 
0.02,0.981981982,0 
0.02,0.004504505,1 
0.02,0.004504505,2 
0.02,0.004504505,3 
0.02,0.004504505,4 
0.2,0.985221675,0 
0.2,0.004926108,1 
0.2,0,2 

、それはそれのようになります。

プロットは、パッケージ ggplot2で作られています

plot

コードは、

です。それはそれを4回を持っているplotspaceの多くを盗むよう、すべての4つのプロットため 1単一の共有凡例を取得することが可能である場合
plot_grid(ponezoom, ptwozoom,pthreezoom,pfourzoom, align='h', labels=c('A', 'B','C','D')) 

は今、私は思っていました。私はどんな助けにも感謝します。

+0

グラフの場合はggplot2、グリッドの場合はcowplot –

+0

各列の下部にある2つの凡例が満足できるかどうか、または単一の凡例である必要があるかどうかを明確にする必要があります。また、いくつかのデータを投稿する必要があります。 –

答えて

3

そこには、これを行う方法を示すis a vignetteがあります。

アプローチは、凡例が非表示のtheme(legend.position="none")でプロットを作成することです。 これらのオブジェクトの1つから凡例grobを抽出します。

grobs <- ggplotGrob(pfour)$grobs 
legend <- grobs[[which(sapply(grobs, function(x) x$name) == "guide-box")]] 

次に、凡例を別の「プロット」としてプロットします。 右側に伝説を表示するには、次のようにします。

# build grid without legends 
pgrid <- plot_grid(pone, ptwo, pthree, pfour, ncol = 2) 
# add legend 
p <- plot_grid(pgrid, legend, ncol = 2, rel_widths = c(1, .1)) 
+1

'cowplot'に' get_legend(p) '関数があります(あなたがリンクしているビネットにリストされています)。 –

関連する問題