ggplot2. How to make geom_bar stacked chart y-range 0-100%?

matt

When using geom_bar with stat = "identity", the y-axis max is the sum total of all values. In this example, I want the y-axis max to be 100 rather than 300 and the stacked bar to show the proportion of the bar each replicate is. Does anyone know how I can do this?

dat = data.frame(sample = c(rep(1, 12),
                            rep(2, 9),
                            rep(3, 6)),
                 category = c(rep(c("A", "B", "C"), each = 4),
                              rep(c("A", "B", "C"), each = 3),
                              rep(c("A", "B", "C"), each = 2)),
                 replicate = c(rep(c("a", "b", "c", "d"), 3),
                               rep(c("a", "b", "c"), 3),
                               rep(c("a", "b"), 3)),
                 value = c(rep(25, 12),
                           rep(c(25, 25, 50), 3),
                           rep(50, 6))
                 )

ggplot(dat, 
       aes(x = sample, y = value)) +
  geom_bar(aes(fill = replicate),
           stat = "identity")

Stacked bar with incorrect y-axis

Ronak Shah

One way would be to pre-calculate the values before plotting.

library(dplyr)
library(ggplot2)

dat %>%
   group_by(sample) %>%
   mutate(value = value/sum(value) * 100) %>%
   ggplot() + aes(x = sample, y = value, fill = replicate) +
   geom_col()  +
   ylab('value  %')

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to control ordering of stacked bar chart using identity on ggplot2

How to get rid of wider bars of a ggplot2 plot with geom_bar() in cases of count=0

Advanced stacked bar chart ggplot2

how to make single stacked bar chart in ggplot2

Manually change order of y axis items on complicated stacked bar chart in ggplot2

How to get a common legend for stacked geom_bar and geom_point in ggplot2?

How modify stacked bar chart in ggplot2 so it is diverging

How do I make pandas catagorical stacked bar chart scale to 100%

How to make horizontal bar chart using seaborn to be stacked to a 100% by hue parameter?

Grouped stacked bar chart in ggplot2 where each stack corresponds to its y axis value

Change order in y-direction of stacked bar chart in ggplot2 as indicated by sequence

How to make a stacked bar chart in matplotlib?

highcharts- stacked bar, how can I make the chart cover the width 100%?

How to display 0 value in a bar chart using ggplot2

How to organize percentage values on top of a stacked bar chart ggplot2

Adjusting y axis origin for stacked geom_bar in ggplot2

Rstudio reorder stacked ggplot geom_bar

How can I put labels on geom_bar() in ggplot2 (without a manual y_position)

How to arrange y axis in bar chart using ggplot2

How to make a 100% Stacked Chart with 2 measures in Tableau?

How to created a grouped and stacked bar chart in R with ggplot2

how to change the stacked bar chart using ggplot2 (percentage, sort) in R

How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

Grafana 100% stacked bar chart

how to plot 100% bar chart from a stacked chart with plotly?

Create a 100% stacked bar chart

How do I reorder a stacked bar chart in ggplot2 by the value of one portion of the stack?

R & ggplot2: 100% geom_bar + geom_line for average using secondary y axis

How to make stacked bar chart with annotations