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):
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?
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.
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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With