繁体   English   中英

在ggplot2 / geom_bar中使用group + facet_wrap填充被忽略

[英]Fill being ignored with group + facet_wrap in ggplot2 / geom_bar

我怀疑我可能在这里使用的组不正确,但是我似乎无法理解为什么在下面的示例中填充颜色会被忽略。

df <- data.frame(a = factor(c(1,1,2,2,1,2,1,2)), 
                 b = factor(c(1,2,3,4,5,6,7,2)), 
                 c = factor(c(1,2,1,2,1,2,1,2)))
p <- ggplot(df, aes(x=b)) +
  geom_bar(aes(y =  ..density.., group = c, fill=a), binwidth = 1) + 
  facet_wrap(~ c) +
  scale_y_continuous(labels = percent_format()) +
  scale_color_hue()
p

任何帮助将不胜感激。 在此先感谢--JT

我想我知道你现在打算做什么。 我会做这样的事情:

df <- data.frame(a = c(1,1,2,2,1,2,1,2), 
             b = c(1,2,3,4,5,6,7,2), 
             c = c(1,2,1,2,1,2,1,2))

df <- within(df, { f <- 1 / ave(b, list(c), FUN=length)})
df[, 1:3] <- lapply(df[, 1:3], as.factor)

ggplot(df, aes(x = b)) + geom_bar(stat = "identity", position = "stack", 
           aes(y = f, group = c, fill = a), binwidth = 1) + facet_wrap(~ c) + 
           scale_y_continuous(labels = percent_format())

这给出了情节:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM