Home > Enterprise >  how to prevent bars from stacking, when a factor is present multiple times?
how to prevent bars from stacking, when a factor is present multiple times?

Time:01-20

I am having issues in preventing same factor from stacking on top of itself when plotting a barplot. Problem is that I don't know how to plot the unique values of such factor and i end up having it n times (n = times the factor is repeated) in the same plot. Here a nice example of the issue:

library("ggplot2")

mydata <- data.frame(var_one=rep(c("a", "b", "c", "d", "e"), 5), var_two=rep(c(1.5, 2, 2.2, 1.8, 1.2), 5))

dev.new()
ggplot(mydata, aes(x=var_one, y=var_two))  
    geom_bar(stat="identity", fill="lightcoral", alpha=.8, width=.4, color="black")

the goal is to have only one of the multiple stacked factors. How to do it? I also tried with geom_col, instead of geom_bar but it doesn't seem to solve the issue.

dev.new()
ggplot(mydata, aes(x=var_one, y=var_two))  
    geom_col(fill="lightcoral", alpha=.8, width=.4, color="black")

This is what i need:

mydata_mod <- data.frame(var_one=c("a", "b", "c", "d", "e"), var_two=c(1.5, 2, 2.2, 1.8, 1.2))

dev.new()
ggplot(mydata_mod, aes(x=var_one, y=var_two))  
    geom_bar(stat="identity", fill="lightcoral", alpha=.8, width=.4, color="black")

but the problem is the data structure that i am using is not in the format of mydata_mod but it is defined as mydata and i have to keep it that way. Ideas?

CodePudding user response:

You could achieve your desired result by using stat_summary or by switching to stat="summary" in geom_bar to compute the sum on the fly:

library("ggplot2")

ggplot(mydata, aes(x = var_one, y = var_two))  
  stat_summary(
    geom = "bar", fun = "sum",
    fill = "lightcoral", alpha = .8, width = .4, color = "black"
  )


ggplot(mydata, aes(x = var_one, y = var_two))  
  geom_bar(
    stat = "summary", fun = "sum",
    fill = "lightcoral", alpha = .8, width = .4, color = "black"
  )

CodePudding user response:

Another option is to wrangle the data:

library(dplyr)
library(ggplot2)

mydata %>% 
  group_by(var_one) %>% 
  summarise(sum = sum(var_two)) %>% 
  ggplot(aes(x = var_one, y=sum))  
  geom_col(fill="lightcoral", alpha=.8, width=.4, color="black")

enter image description here

  • Related