2017-05-08 2 views
5

다음과 같은 문제가 있습니다. 나는 3 인자 변수 대 1 숫자 변수를 barplot 할 필요가있다.그룹화 된 막대 그래프, 하나의 숫자 대 3 계승 변수

내 데이터 세트 :

Site,Gall,Status,Count 
Site1,absent,unhealthy,35 
Site1,absent,healthy,1750 
Site1,present,unhealthy,23 
Site1,present,healthy,1146 
Site2,absent,unhealthy,146 
Site2,absent,healthy,1642 
Site2,present,unhealthy,30 
Site2,present,healthy,333 

내가 ggplot를 사용하여 시도했다, 그러나 그것은 단지 나 x, y 및 한 번 더 옵션을 정의 할 수 있습니다, 그래서 = 담즙 채우기를 사용했다.

내 코드는 다음과 같이 보입니다. 아직 변수가 하나 빠져 있습니다.

ggplot(dat, aes(Status, Count, fill = Gall)) + 
    geom_bar(stat = "identity", position = "dodge") 

아무도 도와 줄 수 있습니까?

고맙습니다.

+1

당신은 세 번째 요소를 포함하도록 매핑 '그룹 = Site' 같은 것을 추가 할 수 있습니다. 'facet_wrap'이 수많은 정보를 제공하는 더 좋은 방법 일지 모르지만. –

+0

@AdamQuek - 도움이 될만한 것 이상으로, 나는 요소 사이트에 대해 facet_wrap을 사용했으며 정확하게 그것을 원했던 것처럼 보입니다! 나는 너의 시간 남자를위한 순전히 감사한다, 감사한다! – RLover

+0

[ggplot2 막 대형 차트의 여러 하위 그룹] (http://stackoverflow.com/questions/20060949/ggplot2-multiple-sub-groups-of-a-bar-chart)도 참조하십시오. – Henrik

답변

2

두 가지 해결책이 있습니다. 이 두 가지 요인에 의해 작성에 의도가 있다면, 당신은 interaction를 사용할 수 있습니다 일반적으로

ggplot(dat, aes(Status, Count)) + 
    geom_col(aes(fill = interaction(Site, Gall)), position = "dodge") 

enter image description here

하지만, 그것은 여러 요인 패 시팅을 사용하는 것이 좋습니다. 예를 들어 :

ggplot(dat, aes(Status, Count)) + 
    geom_col(aes(fill = Gall), position = "dodge") + facet_grid(Site ~ .) 

enter image description here

2

당신은 포인트 대신 막대가 더 좋을 수 있습니다. 예를 들어 :

library(dplyr) 
library(ggplot2) 

ggplot(dat %>% mutate(Site = gsub("([0-9]$)", " \\1", Site)), 
     aes(Status, Count, colour=Status, shape=Gall)) + 
    geom_point(size=3, position=position_dodge(0.5), stroke=1) + 
    facet_grid(~ Site, switch="x") + 
    theme_classic() + 
    theme(strip.placement = "outside", 
     strip.background=element_blank()) + 
    scale_colour_manual(values=hcl(c(195,15),100,65)) + 
    scale_shape_manual(values=c(1,16)) + 
    labs(x="") + 
    guides(colour=FALSE) 

enter image description here

+0

감사합니다 모두, 나는 그런 빠르고, 상세한 도움이되는 대답을 기대하지 않았다! 처음으로이 포럼을 사용하고 있으며 놀랍습니다. – RLover

관련 문제