每个类别,每个变量百分比的条形图

时间:2013-02-27 17:12:49

标签: r plot ggplot2

给出以下示例数据:

df<-data.frame(cbind(cntry<- c("BE","ES","IN","GE","BE","ES","GE",NA,"IN","IN"),
gndr<- c(NA,1,2,2,2,2,1,1,1,2),
plcvcrcR<-c(0,1,NA,0,0,1,1,1,0,0),
plcpvcrR<-c(0,1,1,1,NA,0,0,0,0,0),
plccbrgR<- c(0,1,0,NA,0,1,0,1,1,0),
plcarcrR<-c(1,0,0,NA,1,0,1,0,0,0),
plcrspcR<-c(1,1,0,0,0,0,0,1,1,NA)))
colnames(df)<- c("cntry", "gndr", "plcvcrcR", "plcpvcrR", "plccbrgR", "plcarcrR", "plcrspcR")
df

我如何制作条形图,例如每个性别(gndr)显示变量1-valuesplcpvcrRplccbrgRplcarcrR的百分比?优选地,针对每个性别的条形被分组,并且针对不同变量具有不同颜色的条形。

像这样的图像,其中一种颜色指的是问题,而一组颜色指的是性别(没有置信区间):

https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSsAlUJsqdhxXHiY35FxFmVx3BREVji_ca24w9ub_OYEfZ3O50X5Q

我已经尝试了以下功能,其中我知道它包含许多缺陷:

barplot(((colSums(df[c(3:5)], na.rm=TRUE)/nrow(df[c(3:5)]))*100)~gndr)

1 个答案:

答案 0 :(得分:3)

我会做这样的事情:

require(ggplot2)
require(reshape2)
require(scales)
require(plyr)

# remove NA from gndr
df <- df[!is.na(df$gndr), ]
# now get percentages
df.o <- ddply(df, .(gndr), summarise, 
              plcpvcrR = sum(plcpvcrR == 1, na.rm = T)/sum(!is.na(plcpvcrR)), 
              plccbrgR = sum(plccbrgR == 1, na.rm = T)/sum(!is.na(plccbrgR)), 
              plcrspcR = sum(plcrspcR == 1, na.rm = T)/sum(!is.na(plcrspcR)))
# melt it:
df.m <- melt(df.o, id.var = "gndr")

# plot it:
ggplot(data = df.m, aes(x=gndr)) + geom_bar(aes(weights=value, fill=variable), 
      position = "dodge") + scale_y_continuous(labels=percent)

获得百分比可能更容易/更直接。这是情节:

enter image description here