ggplot2에서 그래프를 만들려고합니다. 이 페이지의 그래프와 비슷한 백분율로 누적 가로형 막 대형 차트를 만들려고 노력하고 있지만 막대에 백분율 레이블을 추가하는 데 어려움을 겪고 있습니다. How to draw stacked bars in ggplot2 that show percentages based on group?누적 막 대형 차트에 백분율 레이블 추가 ggplot2
모든 답변을 추가하고 추가했습니다. 비율 레이블 코드와 유사한 것을 사용
geom_text (AES (라벨 = 라벨), 위치 = position_stack (vjust = 0.5), 크기 = 2)
하지만 나를 위해 작동하지 않습니다 .
내 데이터는 다음과 같다 : 가
코드 :
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
내 그래프는 다음과 같다 이 엉망진창에 :
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))
어떤 제안이? 어떤 조언이나지도가 대단히 감사합니다! 고맙습니다!!
당신이 바'안양하기 전에 비율을 계산할 수있다 [3 : 7] DF [7, 3] <그냥 또한 그룹에 대한 유연한 플롯을 생산하는
facet_wrap
를 추가 , 3 : 7])'나는 당신이 더 많은 그룹을 가지고 있다고 생각합니다, 그래서 당신은 "Group" – rawr