2017-01-17 11 views
2

저는 R에 새로운 브랜드입니다. 원형 그래프를 그릴 필요가 있습니다. 지금 나는 최선을 다했지만 나를 위해 원형 차트를 생성하지는 않습니다. 아래는 제 코드입니다.상대 빈도가있는 테이블에서 R에 원형 차트를 플롯하는 방법은 무엇입니까?

socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T) 
rownames(socialIssue) <- c("Housing","Transportation","Health Care","Education","Food","Other") 
colnames(socialIssue) <- c("Frequency") 
socialIssue <- as.table(socialIssue) 
socialIssue/sum(socialIssue) 

cols <- rainbow(nrow(socialIssue)) 
pie(socialIssue$Frequency, labels=paste0(round(socialIssue$Frequency/sum(socialIssue$Frequency)*100,2),"%"),colnames=cols) 

다음 출력은 다음과 같습니다. 출력되는 주파수가 정확합니다.

socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T) 
> rownames(socialIssue) <- c("Housing","Transportation","Health Care","Education","Food","Other") 
> colnames(socialIssue) <- c("Frequency") 
> socialIssue <- as.table(socialIssue) 
> socialIssue/sum(socialIssue) 
       Frequency 
Housing  0.24019608 
Transportation 0.10980392 
Health Care 0.15000000 
Education  0.06960784 
Food   0.13039216 
Other   0.30000000 
> 
> cols <- rainbow(nrow(socialIssue)) 
> pie(socialIssue$Frequency, labels=paste0(round(socialIssue$Frequency/sum(socialIssue$Frequency)*100,2),"%"),colnames=cols) 
Error in socialIssue$Frequency : $ operator is invalid for atomic vectors 

답변

1

dataframe로 변환 한 후이 그것을 수행

socialIssue = as.data.frame(socialIssue) 
socialIssue$percent = round(100*socialIssue$Freq/sum(socialIssue$Freq), digits = 1) 
socialIssue$label = paste(socialIssue$Var1," (", socialIssue$percent,"%)", sep = "") 
pie(socialIssue$Freq, labels = socialIssue$label, col = cols) 

enter image description here

+0

너무 – user372204

2

플롯 :

pie(socialIssue[, 1], 
    labels = paste0(round(socialIssue[, 1]/sum(socialIssue[, 1]) * 100, 2), "%")) 

당신이 매트릭스를 가지고 있기 때문에,없는 데이터 프레임.

+0

주셔서 감사합니다 감사합니다 ...하지만 난 내 그래프의 제목을 작성하고 또한 하단의 범례를 표시 할 수있는 방법이있다. 범례에는 범주가 나와야하며 백분율은 원형 차트에 표시되어야합니다. – user372204

2

prop.table은 숫자 값의 형식 지정을 처리하므로 sprintf은 소수점 이하 자리를 유지합니다. 전설 추가와 함께, 당신이 사용하는 색상으로

기본 R로
socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T) 
pie(socialIssue, labels=sprintf("%.2f%%", prop.table(socialIssue)*100)) 
1

은 (매개 변수 이름 대신`COLNAMES '의 cols해야한다) :

변환 코드의 모든

중 하나 필요하지 않습니다

pie(socialIssue[,1], labels=paste0(round(socialIssue/sum(socialIssue)*100,2),"%"),col=cols) 
legend('bottomright', legend=rownames(socialIssue), fill=cols, bty='n') 

enter image description here

또는 함께 ggplot2

socialIssue <- matrix(c(245,112,153,71,133,306),ncol=1,byrow=T) 
rownames(socialIssue) <- c("Housing","Transportation","Health Care","Education","Food","Other") 
colnames(socialIssue) <- c("Frequency") 
library(ggplot2) 
library(scales) 
ggplot(as.data.frame(socialIssue), aes(x='',y=Frequency, fill=as.factor(Frequency))) + 
    geom_bar(width=1, stat='identity') + 
    scale_fill_manual(values=cols, labels=rownames(socialIssue)) + 
    scale_y_continuous(labels=percent) + 
    coord_polar(theta = "y") + theme_bw() 

enter image description here

+0

감사합니다. – user372204