简体   繁体   English

ggplot堆叠的条形图,每个堆叠类别中的Alpha差异

[英]ggplot Stacked Bar Chart with Alpha Differences within Each Stacked Category

My question adds to the answer submitted here 4 years ago. 我的问题增加了4年前在这里提交的答案。 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")

在此处输入图片说明

Here is an extremely crude MS Paint drawing of what I am attempting: 这是我正在尝试的极其粗糙的MS Paint图:

在此处输入图片说明

I know that opts() is deprecated so I'm not including that going forward. 我知道opts()已过时,因此不包括在内。

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). 我无法弄清楚如何设置最小y和最大y值以将条形图设置为彼此顶部(如果那是我应该走的路线)。 Simply changing the position="dodge" to position="stack" just makes it all overlap each other and incomprehensible. 简单地将position="dodge"更改为position="stack"只会使它们彼此重叠并且难以理解。 I would also prefer to keep the outline only around the ALL values and not the "in" and "out" values, if that makes sense. 我也希望仅在所有值周围保留轮廓,如果有意义的话,而不是在“ in”和“ out”值周围。

Here is the code using data reshape by melt function: 这是使用通过melt函数重塑数据的代码:

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

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

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