ggplot:显示%而不是计数。熔化的data.frame中的多个变量
我读过关于ggplot显示%而不是计数的文章,但当我有一个由多个因子变量组成的数据帧时,我无法让它工作 这给了我一个曲线图,其中条形图仍然显示计数,而不是我想要的百分比ggplot:显示%而不是计数。熔化的data.frame中的多个变量,r,ggplot2,R,Ggplot2,我读过关于ggplot显示%而不是计数的文章,但当我有一个由多个因子变量组成的数据帧时,我无法让它工作 这给了我一个曲线图,其中条形图仍然显示计数,而不是我想要的百分比 # Data head(sabat) V1 V2 V3 V4 V5 V6 V7 V8 1 2 2 2 2 2 2 1 2 2 2 1 2 1 2 1 1 2 3 2 2 2 2 2 2 2 1 4 1 2 2 2 2 2 2
# Data
head(sabat)
V1 V2 V3 V4 V5 V6 V7 V8
1 2 2 2 2 2 2 1 2
2 2 1 2 1 2 1 1 2
3 2 2 2 2 2 2 2 1
4 1 2 2 2 2 2 2 2
5 2 1 2 2 2 2 1 2
6 2 1 2 1 2 1 1 2
# melt data.frame
sabat.m <- melt(sabat)
# set 2 to NA because as only want to plot the yes answers (1=yes, 2=no)
sabat.m$value[sabat.m$value==2] <- NA
sabat.m <- na.omit(sabat.m)
# Factor it
sabat.m$value <- factor(sabat.m$value)
#structure of the melted data.frame
str(sabat.m)
data.frame': 1020 obs. of 2 variables:
$ variable: Factor w/ 8 levels "V1","V2","V3",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : Factor w/ 1 level "1": 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "na.action")=Class 'omit' Named int [1:2412] 1 2 3 5 6 7 8 10 11 12 ...
.. ..- attr(*, "names")= chr [1:2412] "1" "2" "3" "5" ...
# Plot it
p <- ggplot(sabat.m, aes(x = variable)) +
geom_bar(aes(y = (..count..)/sum(..count..)), binwidth = 25) +
scale_y_continuous(labels = percent_format()) +
coord_flip()
p
提前谢谢是的,但据我所知。Formatter仅适用于RMy会话信息的旧版本:R版本2.15.3、scales_0.2.3、ggplot2_0.9.3.1。它正确地绘制了它的坐标轴,但只有坐标轴变为%。这些条形图仍然表示带有样本数据的计数6行数据,代码图显示百分比-对于V1,它是近似值。8%,对于V2 23%等,加总是100%,因为它应该是。只需替换总和..计数。。在带有529的geom_栏中,如果您只想要sabat.m中的1,请使用sabat.m