Home > Blockchain >  ggplot2 grouped bar chart for months over several years is producing overlapping bars
ggplot2 grouped bar chart for months over several years is producing overlapping bars

Time:02-11

I am trying to make a grouped bar chart and the bars are crowded and overlapping for my x-values. How do I get the grouped bar chart to plot correctly?

I've tried formatting the date with as.yearmon to sort the table for graphing; I have also formatted the table with year and month in separate columns for ease of graphing. However, neither format has worked for me. I have tried as.date and lubridate also and have had no luck so far.

Here is what my data frame looks like:

structure(list(Month = c("August", "July", "June", "May", "November", 
"October", "September", "April", "August", "December", "February", 
"July", "June", "March", "May", "November", "October", "September", 
"April", "August", "December", "February", "January", "July", 
"June", "March", "May", "November", "October", "September", "April", 
"August", "February", "January", "July", "June", "March", "May", 
"November", "October", "September", "January", "August", "July", 
"June", "May", "November", "October", "September", "April", "August", 
"December", "July", "June", "March", "May", "November", "October", 
"September", "April", "August", "December", "February", "January", 
"July", "June", "March", "May", "November", "October", "September", 
"April", "August", "February", "January", "July", "June", "March", 
"May", "November", "October", "September", "January", "August", 
"July", "June", "May", "November", "October", "September", "April", 
"August", "December", "February", "July", "June", "March", "May", 
"November", "October", "September", "April", "August", "December", 
"February", "January", "July", "June", "March", "May", "November", 
"October", "September", "April", "August", "February", "January", 
"July", "June", "March", "May", "November", "October", "September", 
"January"), Year = c(2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2020L, 
2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2020L, 2016L, 2016L, 2016L, 2016L, 
2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 
2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 
2019L, 2020L), DSG.Station = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("9M", "14M", "37M"
), class = "factor"), Richness = c(3.75, 3, 11.5, 6.16666666666667, 
0, 0, 1.5, 4.25, 5.75, 3.5, 0, 8, 9.5, 2.6, 7.75, 5.25, 9.16666666666667, 
6.25, 6.5, 10, 7, 4, 3.5, 8, 11, 5.75, 7, 7.66666666666667, 5.66666666666667, 
8.33333333333333, 7, 6, 4.5, 3.5, 10, 5.5, 8.5, 10.5, 2, 5.5, 
12.5, 5, 5, 5.5, 6, 5.66666666666667, 0.5, 1.5, 2, 4.5, 9, 5.16666666666667, 
5.66666666666667, 7, 0.5, 8, 7.25, 6.5, 5, 6.5, 6, 5, 4.75, 3.25, 
7.5, 10, 6.5, 8.25, 7, 8, 6.66666666666667, 11, 10.5, 9.5, 5, 
7.5, 6.5, 4.5, 10, 4.5, 3.5, 8.5, 3, 4, 4.5, 9, 10, 1.8, 3.25, 
1, 5.5, 6, 3.5, 1, 7, 5, 2.5, 6.66666666666667, 3.75, 5.5, 6.5, 
7, 9.5, 4, 6.25, 3.5, 8.5, 11.3333333333333, 6.75, 6.5, 5, 8.66666666666667, 
7.33333333333333, 9, 9, 4.5, 3, 9, 13.5, 6.5, 9, 3.5, 3, 11, 
6.5), yrmn = c("2016 August", "2016 July", "2016 June", "2016 May", 
"2016 November", "2016 October", "2016 September", "2017 April", 
"2017 August", "2017 December", "2017 February", "2017 July", 
"2017 June", "2017 March", "2017 May", "2017 November", "2017 October", 
"2017 September", "2018 April", "2018 August", "2018 December", 
"2018 February", "2018 January", "2018 July", "2018 June", "2018 March", 
"2018 May", "2018 November", "2018 October", "2018 September", 
"2019 April", "2019 August", "2019 February", "2019 January", 
"2019 July", "2019 June", "2019 March", "2019 May", "2019 November", 
"2019 October", "2019 September", "2020 January", "2016 August", 
"2016 July", "2016 June", "2016 May", "2016 November", "2016 October", 
"2016 September", "2017 April", "2017 August", "2017 December", 
"2017 July", "2017 June", "2017 March", "2017 May", "2017 November", 
"2017 October", "2017 September", "2018 April", "2018 August", 
"2018 December", "2018 February", "2018 January", "2018 July", 
"2018 June", "2018 March", "2018 May", "2018 November", "2018 October", 
"2018 September", "2019 April", "2019 August", "2019 February", 
"2019 January", "2019 July", "2019 June", "2019 March", "2019 May", 
"2019 November", "2019 October", "2019 September", "2020 January", 
"2016 August", "2016 July", "2016 June", "2016 May", "2016 November", 
"2016 October", "2016 September", "2017 April", "2017 August", 
"2017 December", "2017 February", "2017 July", "2017 June", "2017 March", 
"2017 May", "2017 November", "2017 October", "2017 September", 
"2018 April", "2018 August", "2018 December", "2018 February", 
"2018 January", "2018 July", "2018 June", "2018 March", "2018 May", 
"2018 November", "2018 October", "2018 September", "2019 April", 
"2019 August", "2019 February", "2019 January", "2019 July", 
"2019 June", "2019 March", "2019 May", "2019 November", "2019 October", 
"2019 September", "2020 January")), row.names = c(NA, -125L), class = "data.frame")

Here is my code for the plot:

ggplot(rich.mean2, aes(factor(DSG.Station), fill=DSG.Station, y=Richness, x=yrmn))   
  geom_bar (position = position_dodge(2.5), width= 2, colour= 'black', stat='identity') 
  ggtitle('Richness Per Month') 
  theme(axis.text = element_text(size=7, colour =  'black'), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black" )) 
  scale_y_continuous(expand = c(0,0)) 
  scale_x_discrete(limits=rich.mean2$yrmn)

You can see how the plot is very overlapping. The error I am getting is:

Warning message: position_dodge requires non-overlapping x intervals

CodePudding user response:

To plot the data in the correct order, we can transform the months into a factor using month.name (built into base R). Then, we can arrange by Year and Month, and combine them into one column, yrmn. Now that they are in order, we can set the order with factor. Now, we can plot as normal, but can set the spacing between groups to width = 0.7 and increase spacing between bars with position_dodge, where we can specify the distance between bars. Note: You cannot increase the normal width parameter too much when using position_dodge as it will push the bars into the other x-intervals, which is the reason for the warning message (i.e., position_dodge requires non-overlapping x intervals).

library(tidyverse)

rich.mean2 %>% 
  mutate(Month = factor(Month, levels = month.name)) %>% 
  arrange(Year, Month) %>% 
  unite(yrmn, c(Month:Year), sep = " ") %>% 
  mutate(yrmn = factor(yrmn, levels = unique(yrmn))) %>% 
  ggplot(., aes(x = yrmn, y = Richness, fill = DSG.Station))  
  geom_bar(stat = "identity",
           width = 0.7,
           position = position_dodge(width = 0.8))  
  theme(axis.text.x = element_text(
    angle = 90,
    vjust = 0.5,
    hjust = 1
  ))  
  ggtitle('Richness Per Month')  
  theme(
    axis.text = element_text(size = 7, colour =  'black'),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.line = element_line(colour = "black"),
    axis.text.x = element_text(
      angle = 90,
      vjust = 0.5,
      hjust = 1
    )
  )  
  scale_y_continuous(expand = c(0, 0))

Output

enter image description here

Update

For filling in missing months, we can use base R month.name within complete from tidyr. Next, I convert the month name to a number and add the leading 0, which is needed for the date format. Next, I combine Year, Month, and day (i.e., "01"). Then, I arrange by yrmn. Then, I convert to just show month-year. Then, if you want to go back to names rather than month numbers, then we can convert it back. However, if you are fine with date numbers (i.e., 09-2016), then you can comment out the line with separate and the 2 subsequent lines (i.e., through unite). So, now you will see, gaps where there is no data for a given month.

df %>% 
  complete(Month = month.name, nesting(Year), fill = list(Richness = NA, DSG.Station = NA)) %>% 
  mutate(Month = formatC(match(Month, month.name), width = 2, format = "d", flag = "0")) %>% 
  mutate(yrmn = paste(Year, Month, "01", sep="-")) %>% 
  arrange(yrmn) %>% 
  mutate(yrmn = format(as.Date(yrmn, format="%Y-%m-%d"), "%m-%Y")) %>% 
  separate(yrmn, c("Month", "Year"), sep = "-") %>% 
  mutate(Month = with(., month.abb[as.integer(str_remove(Month, "^0 "))])) %>% 
  unite("yrmn", c(Month, Year), sep = " ") %>% 
  mutate(yrmn = factor(yrmn, levels = unique(yrmn))) %>% 
  ggplot(., aes(x = yrmn, y = Richness, fill = DSG.Station))  
  geom_bar(stat = "identity",
           width = 0.7,
           position = position_dodge(width = 0.8))  
  theme(axis.text.x = element_text(
    angle = 90,
    vjust = 0.5,
    hjust = 1
  ))  
  ggtitle('Richness Per Month')  
  theme(
    axis.text = element_text(size = 7, colour =  'black'),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),
    axis.line = element_line(colour = "black"),
    axis.text.x = element_text(
      angle = 90,
      vjust = 0.5,
      hjust = 1
    )
  )  
  scale_y_continuous(expand = c(0, 0))

Output

enter image description here

  • Related