2017-12-22 31 views
2

ggplot2에서 그래프를 만들려고합니다. 이 페이지의 그래프와 비슷한 백분율로 누적 가로형 막 대형 차트를 만들려고 노력하고 있지만 막대에 백분율 레이블을 추가하는 데 어려움을 겪고 있습니다. How to draw stacked bars in ggplot2 that show percentages based on group?누적 막 대형 차트에 백분율 레이블 추가 ggplot2

모든 답변을 추가하고 추가했습니다. 비율 레이블 코드와 유사한 것을 사용

geom_text (AES (라벨 = 라벨), 위치 = position_stack (vjust = 0.5), 크기 = 2)

하지만 나를 위해 작동하지 않습니다 .

내 데이터는 다음과 같다 : 가 graph1

코드 :

melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>% 
ggplot(aes(x=County,y=value,fill=Counties))+ 
    geom_bar(stat = "identity",position="fill", color="black", width=0.9) + 
    labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent) 

geom_text() 코드를 사용 후에는 회전 비율없이

County Group Plan1 Plan2 Plan3 Plan4 Plan5 Total 
County1 Group1 2019 597 513 5342 3220 11691 
County2 Group1 521 182 130 1771 731 3335 
County3 Group1 592 180 126 2448 1044 4390 
County4 Group1 630 266 284 2298 937 4415 
County5 Group1 708 258 171 2640 1404 5181 
County6 Group1 443 159 71 1580 528 2781 
County7 Group1 492 187 157 1823 900 3559 
County8 Group1 261 101 84 1418 357 2221 

내 그래프는 다음과 같다 이 엉망진창에 : graph2

01 23,516,

코드 :

melt(df[df$Group=="Group1",],measure.vars = c("Plan1","Plan2","Plan3","Plan4", "Plan5"),variable.name = "Counties",value.name = "value") %>% 
ggplot(aes(x=County,y=value,fill=Counties))+ 
    geom_bar(stat = "identity",position="fill", color="black", width=0.9) + 
    labs(y="Percent", fill="Plan Type") + ylab("Percentage") + coord_flip() + scale_y_continuous(labels=scales::percent)+ 
geom_text(aes(label=paste0(round(value/100),"%")), position=position_stack(vjust=0.5)) 

어떤 제안이? 어떤 조언이나지도가 대단히 감사합니다! 고맙습니다!!

+0

당신이 바'안양하기 전에 비율을 계산할 수있다 [3 : 7] DF [7, 3] <그냥 또한 그룹에 대한 유연한 플롯을 생산하는 facet_wrap를 추가 , 3 : 7])'나는 당신이 더 많은 그룹을 가지고 있다고 생각합니다, 그래서 당신은 "Group" – rawr

답변

1

레이블이 %가 아니라 원시 값이기 때문에 접근 방식이 작동하지 않았습니다. 당신은 자신의 통계를해야 할 :

df <- read.table(text="County Group Plan1 Plan2 Plan3 Plan4 Plan5 Total 
County1 Group1 2019 597 513 5342 3220 11691 
       County2 Group1 521 182 130 1771 731 3335 
       County3 Group1 592 180 126 2448 1044 4390 
       County4 Group1 630 266 284 2298 937 4415 
       County5 Group1 708 258 171 2640 1404 5181 
       County6 Group1 443 159 71 1580 528 2781 
       County7 Group1 492 187 157 1823 900 3559 
       County8 Group1 261 101 84 1418 357 2221", header = TRUE) 

library(tidyverse) 
df %>% 
    filter(Group == "Group1") %>% 
    select(-Total) %>% 
    gather(key = `Plan Type`, value = value, -County, -Group) %>% 
    group_by(County, Group) %>% 
    mutate(Percentage = value/sum(value)) %>% 
    ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) + 
    geom_col(position = position_stack(), color = "black") + 
    geom_text(position = position_stack(vjust = .5)) + 
    coord_flip() + 
    scale_y_continuous(labels = scales::percent_format()) 

편집 : 위의 코드는 더 계획뿐만 아니라 그 이상의 그룹에 대한뿐만 아니라 작동하지만 줄거리가 설명하지 않습니다

./rowSums (안양 [: - :

df %>% 
     filter(Group == "Group1") %>% 
     select(-Total) %>% 
     gather(key = `Plan Type`, value = value, -County, -Group) %>% 
     group_by(County, Group) %>% 
     mutate(Percentage = value/sum(value)) %>% 
     ggplot(aes(x = County, y = Percentage, fill = `Plan Type`, label = paste0(round(Percentage*100), "%"))) + 
     geom_col(position = position_stack(), color = "black") + 
     geom_text(position = position_stack(vjust = .5)) + 
     coord_flip() + 
     scale_y_continuous(labels = scales::percent_format()) + 
     facet_wrap(~Group)