ggplot Stacked Bar Chart with Alpha Differences wi

2019-08-10 21:51发布

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")

enter image description here

Here is an extremely crude MS Paint drawing of what I am attempting:

enter image description here

I know that opts() is deprecated so I'm not including that going forward.

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.

标签: r ggplot2
1条回答
小情绪 Triste *
2楼-- · 2019-08-10 22:09

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")
查看更多
登录 后发表回答