Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create stacked barplot where each stack is scaled to sum to 100%

Tags:

r

ggplot2

I have a data.frame like this:

df <- read.csv(text = "ONE,TWO,THREE                        23,234,324                        34,534,12                        56,324,124                        34,234,124                        123,534,654") 

I want to produce a percent bar plot which looks like this (made in LibreOffice Calc): enter image description here

Thus, the bars should be standarized so all stacks have the same height and sums to 100%. So far all I have been able to get is is a stacked barplot (not percent), using:

barplot(as.matrix(df)) 

Any help?

like image 553
Julio Diaz Avatar asked Mar 05 '12 08:03

Julio Diaz


People also ask

How do I make a 100% stacked bar chart?

A 100% stacked bar chart requires three data columns or rows from your DataSet—one for categories, one for the series in each category, and one for values. For information about value, category, and series data, see Understanding Chart Data.

What is a stacked 100% bar chart?

In a 100% stacked bar chart, the bars are split into colored bar segments placed on top of each other. Each bar height is 100%, and the colored bar segments represent the components' relative contributions to the total bar.


1 Answers

Here's a solution using that ggplot package (version 3.x) in addition to what you've gotten so far.

We use the position argument of geom_bar set to position = "fill". You may also use position = position_fill() if you want to use the arguments of position_fill() (vjust and reverse).

Note that your data is in a 'wide' format, whereas ggplot2 requires it to be in a 'long' format. Thus, we first need to gather the data.

library(ggplot2) library(dplyr) library(tidyr)  dat <- read.table(text = "    ONE TWO THREE 1   23  234 324 2   34  534 12 3   56  324 124 4   34  234 124 5   123 534 654",sep = "",header = TRUE)  # Add an id variable for the filled regions and reshape datm <- dat %>%    mutate(ind = factor(row_number())) %>%     gather(variable, value, -ind)  ggplot(datm, aes(x = variable, y = value, fill = ind)) +      geom_bar(position = "fill",stat = "identity") +     # or:     # geom_bar(position = position_fill(), stat = "identity")      scale_y_continuous(labels = scales::percent_format()) 

example figure

like image 181
joran Avatar answered Sep 23 '22 01:09

joran