Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot: Barplot with more than one grouping variables and adjusting the colours for the grouping variables

I am trying to create a barplot (stacked perhaps?) that is similar to either of the following two plots in R with ggplot:

1/ enter image description here

2/ enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would prefer to make a barplot similar to the second plot but to make one similar to the first one would do too.

Here is my sample data:

df <- data.frame(
  year = c(1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,
           1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
           2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,
           2018,2019,2020,2021,2022),
  LS4 = c(0,5,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0),
  LS5 = c(35,45,37,29,23,32,46,35,47,40,
          52,55,46,38,0,6,47,41,45,43,
          42,49,29,25,0,0,0,0,0,0,
          0,0,0,0,0),
  LS7 = c(0,0,0,0,0,0,0,0,0,0,
          0,3,20,24,43,33,42,27,25,26,
          26,38,23,23,28,34,50,56,49,52,
          51,57,54,48,49),
  LS8 = c(0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,33,60,61,58,53,
          58,60,55,53,49),
  LS9 = c(0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,
          0,0,0,6,52),
  obs = c(35,50,37,29,23,32,46,35,47,40,
          52,58,66,62,43,39,89,68,70,69,
          68,87,52,48,28,67,110,117,107,105,
          109,117,109,107,150))

I have tried the following to create barplot

barplot1 <- data %>%
  ggplot(aes(x=year, y=obs, fill=interaction(LS4,LS5,LS7,LS8,LS9))) +
  geom_bar(stat="identity", position=position_dodge()) +
  scale_color_manual(values = c("blue", "cyan", "red", "orange"))
barplot1

And the result looks like this

enter image description here

Could someone please help me with this question 🙂 I really appreaciate it

>Solution :

You could convert your data to a longer format using pivot_longer and assign the names to fill with its values. You could use geom_col to create the stacked bars. If you want to color your bars you should use scale_fill_manual since you use fill in the aesthetics to color bars like this:

library(tidyverse)
df %>%
  pivot_longer(cols = LS4:LS9) %>%
  ggplot(aes(x=year, y=value, fill=name)) +
  geom_col() +
  scale_fill_manual(values = c("blue", "cyan", "red", "orange", "yellow"))

Created on 2023-04-03 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading