Search code examples
rggplot2time-seriesstacked-chart

Plotting a stacked bar chart for according to a quarterly time series


I've been working with antibiotic use data over a 6 year period and have managed to plot a bar chart with total amount of antibiotic use per quarter for each year. However, I now need to do the same but with the totals for each antibiotic class used per quarter. I was wanting to do this by making a stacked bar chart (each quarter bar will show x amount of each class used in that quarter).

Some example data from the dataset is;

Date Antibiotic.Total Total.Cephalosporin Total.AminoglycosideTotal.B.Lactam
1   14/05/2013  0.60    0.60    0.00    0.00    
2   03/07/2013  1.20    1.20    0.00    0.00    
3   02/08/2013  4.62    0.00    2.82    1.80    
4   02/08/2013  0.60    0.60    0.00    0.00    
5   20/08/2013  1.20    1.20    0.00    0.00    
6   14/09/2013  1.20    1.20    0.00    0.00    
7   21/10/2013  0.90    0.90    0.00    0.00    
8   14/11/2013  0.60    0.60    0.00    0.00    
9   14/11/2013  2.25    2.25    0.00    0.00
10  01/05/2014  0.60    0.60    0.00    0.00    
11  29/08/2014  0.50    0.50    0.00    0.00    
12  11/09/2014  6.00    4.00    2.00    0.00    
13  11/09/2014  1.20    0.80    0.40    0.00    
14  11/09/2014  0.60    0.60    0.00    0.00    
15  02/10/2014  20.00   0.00    0.00    0.00    
16  20/10/2014  2.58    0.00    1.98    0.00    
17  20/10/2014  5.00    5.00    0.00    0.00    
18  02/04/2015  1.20    1.20    0.00    0.00    
19  24/04/2015  31.50   0.00    17.50   14.00   
20  29/04/2015  0.45    0.45    0.00    0.00    
21  30/04/2015  31.50   0.00    17.50   14.00   
22  03/05/2015  18.00   0.00    10.00   8.00    
23  03/05/2015  45.00   0.00    25.00   20.00

I have been able to plot the antibiotic total quarterly using the following example code;

datapractice %>%
mutate(Q = lubridate::quarter(datepractice, with_year = T)) %>%
group_by(Q) %>% 
summarize(Antibiotic_by_Q = sum(Antibiotic.Total)) %>% 
ggplot(aes(Q, Antibiotic_by_Q)) + geom_bar(stat = "identity", fill = 
"steelblue") + scale_x_yearqtr(format = "%Y") + ylab("Antibiotic Total 
(Grams)") + xlab("Date (Quarters/Year)") +
labs(title = "Antibiotic Use Over a 6 Year Period"

I have tried to alter the above code by altering the line of code beginning with summarize( to show the following

`summarize(Antibiotics_by_Q = sum(Total.Cephalosporin, 
Total.Aminoglycoside, Total.B.Lactam)) %>%`

Alas... this has not worked :(

p.s. see the attached image for the plot of quarterly antibiotic totals...

bar plot

Any help to try and format the bar chart I've already made to show the data as a stacked bar plot would be much appreciated! :)


Solution

  • I am not sure that the following is what the question asks for, but the trick is to summarise_if the columns are numeric, then reshape from wide to long format.

    I have removed the custom color since there are now 4 colors.

    library(tidyverse)
    library(zoo)
    library(lubridate)
    
    datapractice %>%
      mutate(Date = dmy(Date),
             Q = quarter(Date, with_year = TRUE)) %>%
      group_by(Q) %>% 
      summarise_if(is.numeric, sum, na.rm = TRUE) %>%
      gather(Key, Total, -Q) %>%
      ggplot(aes(Q, Total, fill = Key)) +
      geom_bar(stat = "identity") +
      scale_x_yearqtr(format = "%Y") + 
      ylab("Antibiotic Total (Grams)") + 
      xlab("Date (Quarters/Year)") +
      labs(title = "Antibiotic Use Over a 6 Year Period")