ggplot堆叠的条形图,每个堆叠类别中的Alpha差异 [英] ggplot Stacked Bar Chart with Alpha Differences within Each Stacked Category
问题描述
我的问题添加到提交的答案中在这里 4年前.用户已经创建了闪避的堆积条形图,我想知道如何/是否可以针对每个结果"级别将这些闪避设置为彼此重叠.
My question adds to the answer submitted here 4 years ago. The user has created dodged stacked bar charts and I'm wondering how/if it's possible to set these dodges on top of one another for each "outcome" level.
这是用户的答案和代码:
Here is the user's answer and code:
library(ggplot2)
library(plyr)
N <- 50*(2*8*2)
outcome <- sample(ordered(seq(8)),N,replace=TRUE,prob=c(seq(4)/20,rev(seq(4)/20)) )
category2 <- ifelse( outcome==1, sample(c("yes","not"), prob=c(.95,.05)), sample(c("yes","not"), prob=c(.35,.65)) )
dat <- data.frame(
category1=rep(c("in","out"),each=N/2),
category2=category2,
outcome=outcome
)
# Aggregate
dat.agg <- ddply(dat, .var = c("category1", "outcome"), .fun = summarise,
cat1.n = length(outcome),
yes = sum(category2 %in% "yes"),
not = sum(category2 %in% "not")
)
# Plot - outcome will be x for both layers
ggplot(dat.agg, aes(x = outcome)) +
# First layer of bars - for category1 totals by outcome
geom_bar(aes(weight = cat1.n, fill = category1), position = "dodge") +
# Second layer of bars - number of "yes" by outcome and category1
geom_bar(aes(weight = yes, fill = category1), position = "dodge") +
# Transparency to make total lighter than "yes" - I am bad at colors
scale_fill_manual(value = c(alpha("#1F78B4", 0.5), alpha("#33A02C", 0.5))) +
# Title
opts(title = "A pretty plot <3")
这是我正在尝试的极其粗糙的MS Paint绘图:
Here is an extremely crude MS Paint drawing of what I am attempting:
我知道不推荐使用opts()
,所以我不包括以后的内容.
I know that opts()
is deprecated so I'm not including that going forward.
我不知道如何设置最小y和最大y的值,以将条形图设置为彼此顶部(如果那是我应该走的路线).简单地将position="dodge"
更改为position="stack"
只会使它们彼此重叠并且难以理解.如果可以的话,我还希望仅在所有值周围保留轮廓,而不是在"in"和"out"值周围.
I cannot figure out how to set min y, max y values for setting the bars on top of each other (if that's even the route I should go). Simply changing the position="dodge"
to position="stack"
just makes it all overlap each other and incomprehensible. I would also prefer to keep the outline only around the ALL values and not the "in" and "out" values, if that makes sense.
推荐答案
以下是通过melt函数使用数据整形的代码:
Here is the code using data reshape by melt function:
library(ggplot2)
library(plyr)
N <- 50*(2*8*2)
outcome <- sample(ordered(seq(8)),N,replace=TRUE,prob=c(seq(4)/20,rev(seq(4)/20)) )
category2 <- ifelse( outcome==1, sample(c("yes","not"), prob=c(.95,.05)), sample(c("yes","not"), prob=c(.35,.65)) )
dat <- data.frame(
category1=rep(c("in","out"),each=N/2),
category2=category2,
outcome=outcome
)
# Aggregate
dat.agg <- ddply(dat, .var = c("category1", "outcome"), .fun = summarise,
cat1.n = length(outcome),
yes = sum(category2 %in% "yes"),
not = sum(category2 %in% "not")
)
plotData <- dat.agg[, c("category1", "outcome", "cat1.n", "yes")]
plotData <- melt(plotData, id.vars = c("category1", "outcome"))
plotData$FillColor <- ordered(paste0(plotData$category1, "_", plotData$variable), levels=c("in_cat1.n", "out_cat1.n", "in_yes", "out_yes")) # order it the way you want your values to be displayed on the plot
# Plot - outcome will be x for both layers
ggplot(plotData, aes(x = outcome)) +
# Add the layer
geom_bar(aes(weight = value, fill = FillColor)) +
# Add colors as per your desire
scale_fill_manual(values = c(alpha("#1F78B4", 1), alpha("#1F78B4", 0.5), alpha("#33A02C", 1), alpha("#33A02C", 0.5))) +
# Title
ggtitle("A pretty plot <3")
这篇关于ggplot堆叠的条形图,每个堆叠类别中的Alpha差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!