2017-10-04 11 views
0

개별 샘플이 큰 그룹과 작은 하위 그룹에 속하는 데이터 세트가 있습니다. 각 그룹에는 몇 개의 하위 그룹이 있지만 각 하위 그룹은 하나의 큰 그룹에만 속할 수 있습니다. 마찬가지로 각 샘플은 하나의 하위 그룹에만 속할 수 있으므로 하나의 큰 그룹에 속할 수 있습니다.ggplot을 사용하여 누적 막대 그림에 다중 색상 스케일 사용

나는 두 가지 색상의 의미와 참/거짓 누적 막대 그래프를 만들고 싶어 :

  • 개요 (색상)
  • 채우기가 참/거짓 데이터 인 큰 그룹이지만, 두 음영입니다 큰 그룹 윤곽선 색의

이 내가 원하는 무엇에 가까운, 대신 빛과 어두운 회색의, 나는에 대한 밝은 부분과 어두운 빨간색 빨간색 과일 용, 빛과 녹색 과일에 대한 짙은 녹색, 빛과 진한 파란색 싶습니다 푸른 과일.

fruit <- data.frame(Sample=1:20, 
       Fruit=c(rep("Apple", 3), rep("Strawberry", 2), rep("Grape", 4), 
         rep("Watermelon", 4), rep("Lime", 3), rep("Blueberry", 2), 
         rep("Plum", 2)), 
       Color=c(rep("Red", 9), rep("Green", 7), 
         rep("Blue", 4)), 
       Ripe=c(rep(c(T, F), 10))) 

fruit$Fruit <- factor(fruit$Fruit, unique(fruit$Fruit)) 
fruit$Color <- factor(fruit$Color, unique(fruit$Color)) 

ggplot(fruit, aes(Fruit)) + 
    theme_bw() + 
    geom_bar(stat="count", position="fill", 
      aes(fill=Ripe, color=Color)) + 
    scale_fill_manual(values=c("grey65", "grey85")) + 
    scale_y_continuous(labels=scales::percent) 

example

이 가능합니까? 아니면 더 큰 그룹 구성원을 true/false 값과 시각적으로 구별 할 수있는 더 나은 방법이 있습니까? 감사

+0

알파 미학을 사용하십시오. https://stackoverflow.com/a/33222028/471093 – baptiste

답변

3

편집 :이 각 요소 쌍에 대해 고유 한 색상을 지정 interaction를 사용하여, 그것을 할 수있는 더 올바른 방법 아마도 : fill

ggplot(fruit, aes(Fruit)) + 
    geom_bar(aes(fill=interaction(Color,Ripe), color=Color), stat="count", position="fill")+ 
    scale_y_continuous(labels=scales::percent)+ 
    scale_fill_manual(values=c("pink","lightgreen","lightblue","red", "green","blue"))+ 
    theme_bw() 

enter image description here

지도 색상, 및 Ripe to alpha :

ggplot(fruit, aes(Fruit)) + 
    geom_bar(stat="count", position="fill", 
      aes(fill=Color, color=Color,alpha=Ripe)) + 
    scale_y_continuous(labels=scales::percent)+ 
    scale_alpha_discrete(range=c(.5,1))+ 
    theme_bw() 

enter image description here

+0

고마워요! 나는 알파를 사용하는 모습을 선호한다. – hmg

+0

두 번째 예제에서, y 축을 백분율이 아니라 과일의 "개수"로 나타내려면 어떻게해야합니까? 또한, Ripe는 "counts"... – guidebortoli

+1

@guidebortoli 먼저 aggregate하기 위해'dplyr'을 사용합니다 : 과일 2 <- fruit %> % group_by (Fruit, Ripe) %> 요약 (count = n(), Color = first (Color) scale_fill_manual (values ​​= c ("pink()") ggplot (fruit2, aes (Fruit, count)) + geom_bar (stat = "identity", aes (채우기 = 상호 작용 (색상, Ripe), 색상 = 색상) ","lightgreen ","lightblue ","red ","green ","blue ")) + theme_bw()' – Mako212