2017-09-16 8 views
1

を変更します。は、どのように私は相対的な優位性の種を表示するように積み重ねられた棒グラフを使用していた両面積み重ね棒グラフ

棒グラフは、次のコードで生成されます。次のように数字が見える

RelDom <- RelDom[order(RelDom[,2]),] # rank by column A 
RelDom 

RelDom %>% 
    gather(LU, RD, -species) -> likert 
likert 

likert %>% 
    filter(LU=="A") %>% 
    arrange(RD) %>% .$species -> ind_order 
ind_order 


likert %>% 
    mutate(species=factor(species, levels=ind_order, ordered=TRUE)) %>% 
    mutate(LU=factor(LU, 
         levels=c("A", "C", "B"), ordered=F, 
         labels=c("A", "C", "B"))) -> lik 
lik 


tiff(file = "RD.tiff", height=10, width=20, units="in", res=300, compression="lzw") 
ggplot() + 
    geom_hline(yintercept=0, lwd=1) + 
    geom_bar(data=lik, width=.75, 
      stat="identity", position="stack", 
      aes(x=species, y=RD, fill=LU)) + 

    annotate("text", x = 2, y=-50, label = "Old", size=8) + 
    annotate("text", x = 2, y=70, label = "New", size=8) + 

    scale_x_discrete(limits = rev(levels(lik$species)),expand=c(0,0)) + 
    scale_fill_manual(values=c("darkgreen", "red","blue"), 
        drop=FALSE) + 
    scale_y_continuous(expand=c(0,0), 
        limits=c(-100, 200), 
        breaks=c(-100,-50,0,50,100), 
        labels=c("100","50","0","50","100")) + 
    coord_flip() + 
    xlab("Species") + 
    ylab("Relative Dominance") + 
    theme_bw() + 
    theme(legend.position = c(0.9, 0.1)) + 
    theme(legend.title = element_blank()) + 
    theme(legend.text = element_text(colour="black", size = 14)) + 
    theme(legend.background = element_rect(fill="white", size=0.5, linetype="solid", colour ="black")) + 
    theme(axis.title.x = element_text(vjust=0.5,face="bold", size=16), 
     axis.text.x = element_text(vjust=4, size=14)) + 
    theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18), 
     axis.text.y = element_text(size=14)) + 
    theme(panel.grid.minor=element_blank()) + 
    theme(panel.grid.major.y = element_blank()) + 
    theme(panel.grid=element_blank()) + 
    theme(panel.border = element_rect(size=1, color = "black")) + 
    theme(plot.margin = unit(c(0.2,0.9,0.3,0.2),"lines")) 
dev.off() 

enter image description here

、代わりに水平に積層され、青と赤の、私が希望各青色の列は、それぞれの赤色の列の上に直接にある。合わせたことを彼らは緑色の列の幅と一致するので、これは、青と赤の列の両方が半分だけ彼らの現在の幅であるような方法であるべきです。

また、私は、緑、青、赤の順に表示する凡例を取得するために苦労しています。何かアドバイスを事前に

感謝します。

これは、再生可能なデータである:dput(RelDom):私が正しく理解している場合、緑は今と同じ位置にある必要がありながら

structure(list(species = structure(c(1L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Sp1", "Sp10", 
"Sp11", "Sp12", "Sp13", "Sp14", "Sp15", "Sp2", "Sp3", "Sp4", 
"Sp5", "Sp6", "Sp7", "Sp8", "Sp9"), class = "factor"), A = c(-73.55, 
-72.42, -35.62, -12.45, -8.89, -7.26, -6.6, -6.42, -6.02, -5.26, 
-4.59, -4.31, -3.53, -3.25, -2), B = c(64.54, 88.06, 39.57, 14.64, 
6.6, 10.55, 3.87, 7.35, 5.09, 1.88, 6.84, 10.34, 2.17, 2.36, 
1.36), C = c(47.35, 78.55, 39.35, 21.96, 6.25, 7.64, 3.28, 8.94, 
3, 6.04, 5.16, 3.63, 5.42, 12.34, 5.03)), .Names = c("species", 
"A", "B", "C"), row.names = c(NA, 15L), class = "data.frame") 

答えて

5

あなたは並んであることを青と緑のバーをしたいと思います。

Adodgeの2つと、Astackの2つを作ってください。凡例の色の順序については 、あなたは伝説のAになりたい場合は、Bは、LUでレベルの順序を変更Cscale_fill_manual(values=c("darkgreen", "blue", "red")

library(ggplot2) 
ggplot() + 
    geom_bar(data=lik[lik$LU!="A",], width=.75, 
      stat="identity", position="dodge", 
      aes(x=species, y=RD, fill=LU)) + 
    geom_bar(data=lik[lik$LU=="A",], width=.75, 
      stat="identity", position="stack", 
      aes(x=species, y=RD, fill=LU)) + 
    geom_hline(yintercept=0, lwd=1) + 
    annotate("text", x = 2, y=-50, label = "Old", size=8) + 
    annotate("text", x = 2, y=70, label = "New", size=8) + 

    scale_x_discrete(limits = rev(levels(lik$species)),expand=c(0,0)) + 
    scale_fill_manual(values=c("darkgreen", "blue", "red"), 
        drop=FALSE) + 
    scale_y_continuous(expand=c(0,0), 
        limits=c(-100, 200), 
        breaks=c(-100,-50,0,50,100), 
        labels=c("100","50","0","50","100")) + 
    coord_flip() + 
    xlab("Species") + 
    ylab("Relative Dominance") + 
    theme_bw() + 
    theme(legend.position = c(0.9, 0.1)) + 
    theme(legend.title = element_blank()) + 
    theme(legend.text = element_text(colour="black", size = 14)) + 
    theme(legend.background = element_rect(fill="white", size=0.5, linetype="solid", colour ="black")) + 
    theme(axis.title.x = element_text(vjust=0.5,face="bold", size=16), 
     axis.text.x = element_text(vjust=4, size=14)) + 
    theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18), 
     axis.text.y = element_text(size=14)) + 
    theme(panel.grid.minor=element_blank()) + 
    theme(panel.grid.major.y = element_blank()) + 
    theme(panel.grid=element_blank()) + 
    theme(panel.border = element_rect(size=1, color = "black")) + 
    theme(plot.margin = unit(c(0.2,0.9,0.3,0.2),"lines")) 

.... 
mutate(LU=factor(LU, 
         levels=c("A", "C", "B"), ordered=F, 
         labels=c("A", "C", "B"))) -> lik 

は、だから私はredがちょうどcolor_fill_manual行を変更し、あなたがblueBするCをしたいと思い推測:私は、これはあなたが目的用途に使用するので、この行を意味するものではありませんと仮定します

enter image description here

+0

これはまさに私が望んでいたものです。凡例に関して、 'mutate()'関数または 'color_fill_manual'行の順序を変更すると、データとそれぞれの色が混同されます。これは、Rがデフォルトでアルファベット順に列ラベルを並べるためです(実際の列見出しはA = OG、B = LIL、C = HIL、A、B、Cを使って簡単にできますが、潜在的なアルファベット順の問題)。 – tabtimm

+0

うれしかったです。 '... scale_fill_manual(values = c(" OG "=" darkgreen "、" HIL "=" blue "、" LIL "=" red "))' – missuse

+0

ありがとうございます。私は正しい組み合わせを得るかしながら、しかし、すなわち、HIL =青とLIL =赤、順序はOG、HIL、LILなく、OG、バック私は中レベルの順序を変更することを問題に私を取るLIL、HILですlevel = c( "OG"、 "LIL"、 "HIL")の場合、色とその基礎データはもはや一致しません(凡例は正しいです)。アルファベット順の問題を避けるために、元の.csvファイル内で何かを変更できるかどうか、つまりOG、LIL、HILの代わりにA、B、Cを使用し、OG、LIL、HILの名前をRに変更できますか? – tabtimm

関連する問題