I suspect I might be using group incorrectly here, but I can't seem to understand why the fill color is getting ignored in the example below.
我怀疑我可能在这里错误地使用组,但我似乎无法理解为什么填充颜色在下面的示例中被忽略。
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
Any help would be greatly appreciated. Thanks in advance, --JT
任何帮助将不胜感激。在此先感谢, - .JT
1 个解决方案
#1
1
I think I understand what plot you're after now. I'd do something like this:
我想我明白你现在的情节。我会做这样的事情:
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())
This gives the plot:
这给出了情节:
#1
1
I think I understand what plot you're after now. I'd do something like this:
我想我明白你现在的情节。我会做这样的事情:
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())
This gives the plot:
这给出了情节: