비례 값 (백분율)이있는 누적 막대 차트를 만들려고하지만 동시에 각 막대 위에 절대 값을 표시합니다. 현재로서는 차트의 잘못된 위치에만 값을 표시 할 수 있습니다.
내 data.frame
모습은 다음과 같습니다.
Var1 Freq pltype Num
1 SA 18 A1 2
2 SN 2 A1 4
3 UA 18 A1 1
4 SA 4 A2 2
5 UA 34 A2 1
6 SA 8 A3 2
7 SN 1 A3 4
8 UA 29 A3 1
9 SA 21 A4 2
10 SN 10 A4 4
11 UA 7 A4 1
12 N 2 A5 3
13 SA 14 A5 2
14 SN 1 A5 4
15 UA 21 A5 1
16 SA 11 A6 2
17 SN 1 A6 4
18 UA 26 A6 1
19 SA 3 A7 2
20 SN 16 A7 4
21 UN 19 A7 5
22 N 6 A8 3
23 SA 5 A8 2
24 UA 27 A8 1
지금까지이 코드를 만들었습니다.
#Ordered characters by numbers and plotted them
p1 <- ggplot(f[order(f$Num), ],aes(x=pltype,y=(Freq*100)/sum(data.frame(df[[exp]][1])$Freq),fill=Num))+
geom_bar(stat="identity")
p1+scale_fill_brewer(palette="Pastel1",labels=tp_code)+
theme(axis.text.x = element_text(angle = 30, hjust = 1, vjust=1)) +
xlab("Question")+ylab("Percentage")+geom_text(aes(label=Freq))+
ggtitle(names(df)[exp])
이것은 다음과 같은 누적 막대 차트를 제공합니다.
먼저 "R Graphics Cookbook"의 코드를 사용하여 누적 주파수 합계를 생성했습니다.
ce <- arrange(f, pltype, desc(Freq))
ce <- ddply(ce, "pltype",transform, label_y = cumsum(Freq))
엉망으로 다른 순열을 시도 geom_text
하면 코드가 제대로 작동하지 않습니다. 또한이 코드는 각 범주 내의 절대 값이 아닌 누적 합계를 표시하는 문제가 있습니다.
어떤 도움이라도 대단히 감사하겠습니다!
ggplot 코드를 적용하기 전에 누적 합계와 백분율을 생성합니다.
library(ggplot2)
f <- read.table("I:/ggplot.txt",header=T)
f <- f[order(f$Num),]
f$Num <- as.factor(f$Num)
tp_code <-unique(f$Var1[order(f$Num)])
for(i in 1:length(unique(f$pltype))){
f$Pct[f$pltype==unique(f$pltype)[i]] <- f$Freq[f$pltype==unique(f$pltype)[i]]*100/sum(f$Freq[f$pltype==unique(f$pltype)[i]])
f$cumPct[f$pltype==unique(f$pltype)[i]] <- cumsum(f$Pct[f$pltype==unique(f$pltype)[i]])
}
#Ordered characters by numbers and plotted them
p1 <- ggplot(f,aes(x=pltype,y=Pct,fill=Num))+
geom_bar(stat="identity")
p1+scale_fill_brewer(palette="Pastel1",label=tp_code)+
theme(axis.text.x = element_text(angle = 30, hjust = 1, vjust=1)) +
xlab("Question")+ylab("Percentage")+geom_text(aes(x=pltype,y=cumPct,label=Freq,vjust=1))+
ggtitle(names(df)[exp])
또한을 추가하여 vjust=1
각 막대의 상단 바로 아래에 레이블을 배치했습니다. 이것으로 플레이하고 원하는대로 포맷 할 수 있습니다. 그러면 다음이 생성됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다