Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

r - Staggered and stacked geom_bar in the same figure?

I have the following graph, which is essentially two distributions' histograms plotted alongside each other:

my.barplot <- function( df, title="", ... ) {
  df.count <- aggregate( df$outcome, by=list(df$category1,df$outcome), FUN=length )
  colnames( df.count ) <- c("category1","outcome","n")
  df.total <- aggregate( df.count$n, by=list(df.count$category1), FUN=sum )
  colnames( df.total ) <- c("category1","total")
  df.dens <- merge(df.count, df.total)
  df.dens$dens <- with( df.dens, n/total )
  p <- ggplot( df.dens, aes( x=outcome, fill=category1 ), ... )
  p <- p + geom_bar( aes( y=dens ), position="dodge" )
  p <- p + opts( axis.text.x=theme_text(angle=-90,hjust=0), title=title )
  p
}

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
  )

my.barplot(dat)

existing barchart

I'd like to plot within each bar the proportion belonging to some second category. Absent the need to organize it by the first category, I would just stack the bars. However, I can't figure out how to stack by a second category. Basically within each outcome-category1 bar I want the proportion in category2 to be darker shaded.

Here's a GIMP'd image of what I'm trying to create:

barchart with stacked proportions of category2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Base graphics?!? NEVERRRR

Here's what I've come up with. I admit I had a hard time understanding all your aggregation and prep, so I just aggregated to counts and may have gotten that all wrong - but it seems like you're in a position where it might be easier to start from a functioning plot and then get the inputs right. Does this do the trick?

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

Plot with bars that are simultaneously dodged, overlaid, and transparent


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...