2017-09-16 9 views
1

나는 스택 barchart를 사용하여 상대 우위 종을 표시합니다. 대신 파란색과 수평으로 적재되는 빨강, 내가 좋아하는 것으로,양면 스택 barchart를 수정하는 방법

enter image description here

이제 다음과 같이

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() 

그림은 같습니다

막대 그래프가 다음과 같은 코드로 생성됩니다 각 파란색 열은 해당 빨간색 열 바로 위에 있어야합니다. 이것은 파란색과 빨간색 열이 현재 너비의 절반에 불과하므로 녹색 열의 너비와 일치하도록 결합되어야합니다.

또한 전설이 녹색, 파란색 및 빨간색 순서로 표시되는 데 어려움을 겪고 있습니다.

사전에 많은 조언을 주신 것에 대해 많은 감사드립니다.

이는 재생 가능한 데이터는 다음과 같습니다 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이없고, 하나만 Astack이 아닌 두 개가 있습니다. 범례의 색상 순서는 A 인 경우 B, C의 순서를 LU으로 변경하십시오. scale_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 줄을 변경, 당신은 blueBC을하고 싶은 생각 :이 당신이 목적 사용에 당신이 있기 때문에이 선을 의미하는 것이 아니다 가정

enter image description here

+0

대단히 감사합니다. 정확하게 이것은 내가 바라는 것입니다. 전설에 대해,'mutate()'함수 나'color_fill_manual' 라인에서 순서를 바꾸면, 데이터와 각각의 색깔이 혼란스러워집니다. 이것은 R이 기본적으로 알파벳 순서로 열 레이블을 주문하기 때문일 수 있습니다. (필자의 실제 열 제목은 A = OG, B = LIL, C = HIL, A, B, C를 사용하여 더 쉽게 만들 수 있다고 말해야합니다. 잠재적 인 알파벳 순서 문제). – tabtimm

+0

다행이었습니다. 시도해 보셨습니까? ... scale_fill_manual (값 = c ("OG"= "darkgreen", "HIL"= "blue", "LIL"= "red"))' – missuse

+0

감사합니다. 그러나 올바른 조합 (예 : HIL = blue 및 LIL = red)을 얻는 동안 OG, LIL, HIL이 아닌 OG, HIL, LIL이 순서대로 변경됩니다. level = c ("OG", "LIL", "HIL"), 색상 및 기본 데이터가 더 이상 일치하지 않는 경우 (범례 순서가 올바른 경우). OG, LIL, HIL 대신 A, B, C를 사용하고 OG, LIL, HIL로 이름을 바꾸어 알파벳 순서 문제가 발생하지 않도록 원래 .csv 파일에서 무엇인가를 변경할 수 있는지 궁금합니다. – tabtimm