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
706 views
in Technique[技术] by (71.8m points)

Stacked bar chart in R (ggplot2) with y axis and bars as percentage of counts

I'm a novice with ggplot2 and have a question about generating a stacked bar plot. I checked the book and the dedicated webpage, but can't solve the problem. I have two factors, one of which has 2 levels (presence-absence), the other 10 levels. Lets call these two "variable" and "fruit".

I'd like to create a stacked bar plot where each bar reflects a type of fruit and the number of presence-absence observations in "variable" are stacked on top of each other. This is relatively easy (see code for plot 1 below), but I would also like the bars and y axis to express the number of counts of presence-absence in "variable" as a percentage. In other words, all the bars should be the same height (reflecting a total of 100%) and the counts of presence-absence observations should be converted into percentages.

I can change the y axis scale to a percentage using ..count..*100/sum(..count..) but I can't fathom how to convert the actual bars. I created another plot with faceting (code for plot 2 below) that achieves what I want in terms of percentages, but I would prefer the two bars on top of each other. Does anyone have an idea of how to achieve this? I've provided dummy data and reproducible example. Thanks for any help.

Steve

dat <- data.frame( fruit=c("Apple", "Apple", "Orange", "Orange", "Orange", "Orange",
                   "Orange", "Pear", "Pear", "Pear"), variable=c("Present", "Absent",
                   "Present", "Present", "Present", "Present", "Absent", "Absent",
                   "Absent", "Present") )  

# stacked bar plot  
ggplot(dat, aes(x = fruit, fill = variable) ) +  
    geom_bar( aes(y = ..count..*100/sum(..count..) ) ) +
    xlab("Fruit") +
    ylab("Would like this to be percentage") + 
    scale_fill_manual("Condition", values = alpha( c("firebrick", "dodgerblue4"), 1) )  

enter image description here

# with faceting  
ggplot(dat, aes(x = variable, fill = variable) ) +   
    geom_bar( aes(y = ..count..*100/sum(..count..) ) ) +   
    facet_grid(. ~ fruit) +  
    xlab("Fruit") +
    ylab("Would like this to be percentage") + 
    scale_fill_manual("Condition", values = alpha( c("firebrick", "dodgerblue4"), 1) )  

enter image description here

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

For the first graph, just add position = 'fill' to your geom_bar line !. You don't actually need to scale the counts as ggplot has a way to do it automatically.

ggplot(dat, aes(x = fruit)) + geom_bar(aes(fill = variable), position = 'fill')

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

...