2017-11-11 15 views
0

적절한 파이 차트를 플롯하려고합니다. 그러나이 사이트에 대한 이전 질문의 대부분은 stat = identity에서 가져온 것입니다. cut의 비율에 비례하는 각도로 그래프 2와 같은 일반 원형 차트를 그릴 수 있습니까? ggplot2의 diamonds 데이터 프레임을 사용하고 있습니다.ggplot2의 원형 차트 플로팅

ggplot(data = diamonds, mapping = aes(x = cut, fill = cut)) + 
    geom_bar(width = 1) + coord_polar(theta = "x") 

도표 1 enter image description here

ggplot(data = diamonds, mapping = aes(x = cut, y=..prop.., fill = cut)) + 
    geom_bar(width = 1) + coord_polar(theta = "x") 

그래프 2 enter image description here

ggplot(data = diamonds, mapping = aes(x = cut, fill = cut)) + 
    geom_bar() 

그래프 3 enter image description here

답변

3

우리는 먼저 계산할 수 percen 각 cut 그룹의 tage. 이 작업을 위해 dplyr 패키지를 사용했습니다.

library(ggplot2) 
library(dplyr) 

# Calculate the percentage of each group 
diamonds_summary <- diamonds %>% 
    group_by(cut) %>% 
    summarise(Percent = n()/nrow(.) * 100) 

그 후 원형 차트를 플로팅 할 수 있습니다. scale_y_continuous(breaks = round(cumsum(rev(diamonds_summary$Percent)), 1))은 누적 백분율을 기준으로 축 레이블을 설정합니다.

결과는 다음과 같습니다.

enter image description here

+0

이것은 상당히 많은 작업처럼 보입니다. – JetLag