Search code examples
rggplot2geom-barstacked-bar-chart

How to seperate two times stacked bars on top of each other?


I am trying to crate a stacked bar chart but the first two bars are on top of each other instead of beside each other. The other bars are howewer correct.

current Chart

I want to seperate them so that I'll have the 12 months as bars instad of the current 11 (on the x axis).

I checked if there are same values but that's not the case.

ggplot(rawdata4.1, aes(x= Monat, y= Ankünfte, fill= Jahreszahl)) +
  facet_wrap(~ Herkunft, scale = "fixed") +
  geom_bar(position = "stack",
           colour = "black",
            lwd = 0.9,
           linetype = 1,
           stat = "identity",) +
  scale_y_continuous(name= NULL, labels = label_number(suffix = " Mio", scale = 1e-6)) +
  scale_x_binned(name = NULL) +
  ggtitle("Monatliche Ankünfte vor und nach Corona") +
  theme_pander() +
  theme(axis.title.y = element_text(vjust = +1.75), legend.position = "right") +
  scale_fill_economist()

Below are two picture of the whole and final data set and my current code and a picture of my data.

enter image description here

enter image description here


Solution

  • You can use scale_x_continuous with breaks set at 1:12

    library(ggplot2)
    library(ggthemes)
    library(scales)
    
    ggplot(rawdata4.1, aes( x= Monat, y = Ankünfte, fill = Jahreszahl)) +
      facet_wrap(~ Herkunft, scale = "fixed") +
      geom_col(position = "stack", colour = "black", lwd = 0.9, linetype = 1) +
      scale_y_continuous(labels = label_number(suffix = " Mio", scale = 1e-6),
                         name = NULL) +
      scale_x_continuous(NULL, breaks = 1:12) +
      scale_fill_economist() +
      ggtitle("Monatliche Ankünfte vor und nach Corona") +
      theme_pander() +
      theme(axis.title.y = element_text(vjust = +1.75), 
            legend.position = "right") 
    

    enter image description here


    Data transcribed from question in reproducible format

    rawdata4.1 <- data.frame(Jahreszahl = factor(rep(c(2019, 2020), each = 24)),
                     Monat = rep(rep(1:12, each = 2), 2),
                     Herkunft = rep(c("Schweiz", "Alle Länder"), 24),
                     Ankünfte = c(647315, 1226739, 698093, 1332892, 774956, 1526592, 
                                  623656, 1384180, 697684, 1624902, 858811, 2006805, 
                                  987515, 2284423, 1011069, 2270490, 926890, 
                                  1958831,  78940, 1612160, 589729, 1154630, 678702,
                                  1381913, 707576, 1339067, 742885, 1356936, 296702, 
                                  498459, 43310, 53920, 289738, 318331, 606987, 
                                  725924, 1229947, 1574105, 1106300, 1545573, 
                                  1021787, 1304570, 811214, 957811, 353547, 424907, 
                                  484612, 603825))