带填充和组的ggplot2

时间:2014-01-20 21:37:25

标签: r colors ggplot2 grouping

在使用facet_wrap进行分组时,我在填充条形时遇到问题 使用此data.frame:

library(ggplot2)
library(gridExtra)
set.seed(1234)
testDat <- data.frame(answer=factor(sample(c("yes", "no"), 60, replace=TRUE)),
                      which=factor(sample(c("q1", "q2", "q3"), 60, replace=TRUE)))

我想绘制由变量分组的答案。这给了我绝对值:

ggplot(testDat, aes(x=answer)) + 
  geom_bar(aes(fill=answer)) + facet_wrap(~which)

这给了我相对的价值。但不是每组:

ggplot(testDat, aes(x=answer)) + 
  geom_bar(aes(y=(..count..)/sum(..count..), fill=answer)) + facet_wrap(~which)

搜索答案我检测到这个以绘制每组的相对值。但填充颜色不再起作用

ggplot(testDat, aes(x=answer)) + 
  geom_bar(aes(y=(..count..)/sum(..count..), group=which, fill=answer)) + facet_wrap(~which)

它适用于“哪个”而不是“回答”的三个不同值

ggplot(testDat, aes(x=answer)) + 
  geom_bar(aes(y=(..count..)/sum(..count..), group=which, fill=which)) + facet_wrap(~which)

有关如何填充酒吧的任何建议吗?

p1<-ggplot(testDat, aes(x=answer)) + geom_bar(aes(y=(..count..)/sum(..count..), group=which, fill=answer)) + facet_wrap(~which)
p2<-ggplot(testDat, aes(x=answer)) + geom_bar(aes(y=(..count..)/sum(..count..), group=which, fill=which)) + facet_wrap(~which)
grid.arrange(p1,p2)

2 个答案:

答案 0 :(得分:3)

这是你的想法吗?

library(reshape2)
library(ggplot2)
df <- aggregate(answer~which,testDat,
                function(x)c(yes=sum(x=="yes")/length(x),no=sum(x=="no")/length(x)))
df <- data.frame(which=df$which, df$answer)
gg <- melt(df,id=1, variable.name="Answer",value.name="Rel.Pct.")
ggplot(gg) + 
  geom_bar(aes(x=Answer, y=Rel.Pct., fill=Answer),position="dodge",stat="identity")+
  facet_wrap(~which)

不幸的是,当在美学映射中使用时,聚合诸如sum(...), min(...), max(...), range(...)等等的函数不尊重由方面隐含的分组。因此,虽然..count..在单独使用时(在您的分子中)正确地进行了子集化,但sum(..count..)给出了整个数据集的总数。这就是为什么(..count..)/sum(..count..)给出总计的分数,而不是组的分数。

我所知道的唯一方法就是如上所述创建一个腋下表。

答案 1 :(得分:3)

有一种方法可以使用ggplot作为mentioned in this question的请求进行聚合。 但是,它需要使用PANEL variable that isn't documented therefore Hadley recomended not to use it

以下是使用data.table进行汇总的方法。 我还在剧情中添加了百分比标签。

grp <- function(x) {
  percentage = as.numeric(table(x)/length(x))
  list(x = levels(x),
       percentage = percentage,
       label = paste0( round( as.numeric(table(x)/length(x), 0 ) * 100 ), "%")
  )
}

require("data.table")
DT <- data.table(testDat)

# Simpler version
ggplot(DT[, grp(answer), by=which]) +
  geom_bar(aes(x=x, y=percentage, fill = x), position="dodge",stat="identity") +
  facet_grid(~which) + 
  xlab("Answer")

# With percentage labels and y axis with percentage
ggplot(DT[, grp(answer), by=which]) +
  geom_bar(aes(x=x, y=percentage, fill = x), position="dodge",stat="identity") +
  geom_text(aes(x=x, ymax = 0.6, y=percentage, label = label), vjust = -1.2, color = "grey20") +
  facet_grid(~which) + 
  xlab("Answer") + xlim("yes", "no") +
  scale_y_continuous(labels = percent_format()) +
  scale_fill_discrete(name = "Answer")

enter image description here