Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 366
Steve Avatar asked Sep 01 '10 14:09

Steve


People also ask

How do I get the Y-axis percentage in R?

Since we need to add percentages in the labels of the Y-axis, the keyword “labels” is used. Now use scales: : percent to convert the y-axis labels into a percentage. This will scale the y-axis data from decimal to percentage. It simply multiplies the value by 100.

How do you add a percentage to a bar plot?

A better way to make the barplot is to add the percentage symbol on the y-axis instead of the fraction we have now. We can use scales package' percent method to add percentage symbol to the y-axis using scale_y_continuous() function. Now our y-axis text has percentage symbols in the barplot.

Can bar charts be used for percentages?

Bar charts show the frequency counts of data Bar charts show the frequency counts of values for the different levels of a categorical or nominal variable. Sometimes, bar charts show other statistics, such as percentages.

How many variables are in a stacked bar chart?

A stacked bar chart shows two categorical variables. The first (and primary) variable is shown along the entire length of the bar, and the second variable is represented as stacks within each categorical bar.


1 Answers

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') 
like image 111
Ramnath Avatar answered Oct 08 '22 11:10

Ramnath