Search code examples
rggplot2facet-grid

ggplot facet_grid overlap when years change


  1. How do I make the df plot look like the df1 plot? The df plot has overlap of x-axis content when going from Dec 2019 to Jan 2020. In the df plot, I tried changing expand = c(0,15) which makes the x-axis labels look much better, but the line is not connected between Dec 2019 and Jan 2020.

  2. In df1 plot, how do I remove the border around the years and the line in-between the years on the x-axis label?

library(tidyverse)

# What I have: 2019-Dec and 2020-Jan overlap on the facet
date <- as.Date(c("2019-09-01","2019-10-01","2019-11-01","2019-12-01",
                  "2020-01-01","2020-02-01","2020-03-01","2020-04-01"))
year <- c(2019,2019,2019,2019,2020,2020,2020,2020)
month <- c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr")
sales <- c(100,200,600,200,100,100,800,100)
df <- data.frame(date,year,month,sales)

ggplot(df, aes(x=date, y=sales)) + 
  geom_line() +
  scale_x_date(date_labels=paste(c(rep(" ",11), "%b"), collapse=""), 
               date_breaks="month", expand=c(0,0)) +
  facet_grid(~ year(date), space="free_x", scales="free_x", switch = "x") +
  theme_bw() + 
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA,colour="grey50"),
        panel.spacing=unit(0,"lines"), 
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        panel.margin.x=unit(0, "cm"))

#What I want
df1 <- structure(list(Date = structure(c(-91455, -91454, -91453, -91452, 
                                         -91451, -91450, -91448, -91447, -91446, -91445, -91444, -91443, 
                                         -91441, -91440, -91439, -91438, -91437, -91436, -91434, -91433, 
                                         -91431, -91430, -91429, -91427, -91426, -91425, -91424, -91423, 
                                         -91422, -91420, -91419, -91418, -91417, -91416, -91415, -91413, 
                                         -91412, -91411, -91410, -91409, -91408, -91406, -91405, -91404, 
                                         -91403, -91402, -91401, -91399, -91398, -91397, -91396, -91395, 
                                         -91394, -91392, -91391, -91390, -91389, -91388, -91387, -91385, 
                                         -91384, -91382, -91381, -91380, -91379, -91377, -91376, -91375, 
                                         -91374, -91373, -91372, -91371, -91370, -91369, -91368, -91367, 
                                         -91366, -91364, -91363, -91362, -91361, -91360, -91359, -91357, 
                                         -91356, -91355, -91354, -91353, -91352, -91350, -91349, -91348, 
                                         -91347, -91346, -91345, -91343, -91342, -91341, -91340, -91339, 
                                         -91338, -91336, -91335, -91334, -91333, -91332, -91331, -91329, 
                                         -91328, -91327, -91326, -91325, -91324, -91322, -91321, -91320, 
                                         -91319, -91315, -91314, -91313, -91312, -91311, -91310, -91308, 
                                         -91307, -91306, -91305, -91304, -91303, -91301, -91300, -91299, 
                                         -91298, -91297, -91296, -91294, -91293, -91292, -91291, -91290, 
                                         -91289, -91287, -91286, -91285, -91284, -91283, -91282, -91280, 
                                         -91279, -91278, -91277, -91276, -91275, -91273, -91272, -91271, 
                                         -91270, -91269, -91268, -91266, -91265, -91264, -91263, -91262, 
                                         -91261, -91259, -91258, -91257, -91256, -91255, -91254, -91252, 
                                         -91251, -91250, -91249, -91248, -91247, -91245, -91244, -91243, 
                                         -91242, -91241, -91240, -91238, -91237, -91236, -91235, -91234, 
                                         -91233, -91231, -91230, -91229, -91228, -91227, -91226, -91224, 
                                         -91223, -91222, -91221, -91220, -91219, -91217, -91216, -91215, 
                                         -91214, -91213, -91212, -91210, -91209, -91208, -91207, -91205, 
                                         -91201, -91200, -91199, -91198, -91196, -91195, -91194, -91193, 
                                         -91192, -91191, -91189, -91188, -91187, -91186, -91185, -91184, 
                                         -91182, -91181, -91180, -91179, -91178, -91177, -91175, -91174, 
                                         -91173, -91172, -91171, -91170, -91168, -91167, -91166, -91165, 
                                         -91164, -91163, -91161, -91160, -91159, -91158, -91157, -91156, 
                                         -91154, -91153, -91152, -91151, -91150, -91149, -91147, -91146, 
                                         -91145, -91144, -91143, -91142, -91140, -91139, -91138, -91131, 
                                         -91130, -91129, -91128, -91126, -91125, -91124, -91123, -91122, 
                                         -91121, -91119, -91118, -91117, -91116, -91115, -91114, -91112, 
                                         -91111, -91110, -91109, -91108, -91107, -91104, -91103, -91102, 
                                         -91101, -91100, -91099, -91097, -91096, -91095, -91094, -91093, 
                                         -91091, -91090, -91089, -91088, -91087, -91086, -91084, -91083, 
                                         -91082, -91081, -91080, -91079, -91077, -91076, -91075, -91074, 
                                         -91073, -91072, -91070, -91069, -91068, -91065, -91063, -91062, 
                                         -91061, -91060, -91059, -91058, -91056, -91055, -91054, -91053, 
                                         -91052, -91051, -91049, -91048, -91047, -91046, -91045, -91044, 
                                         -91042, -91041, -91040, -91039, -91038, -91037, -91035, -91034, 
                                         -91033, -91032, -91031, -91030, -91028, -91027, -91026, -91025, 
                                         -91024, -91023, -91021, -91020, -91019, -91018, -91017, -91016, 
                                         -91014, -91013, -91012, -91011, -91010, -91009, -91007, -91006, 
                                         -91005, -91004, -91003, -91002, -91000, -90999, -90998, -90997, 
                                         -90996, -90995, -90993, -90992, -90991, -90990, -90989, -90988, 
                                         -90986, -90985, -90984, -90983, -90982), class = "Date"), value = c(113, 
                                                                                                             113, 113, 113, 114, 114, 114, 115, 115, 115, 116, 116, 116, 116, 
                                                                                                             117, 117, 117, 117, 116, 117, 116, 116, 116, 117, 117, 117, 117, 
                                                                                                             117, 117, 117, 116, 117, 116, 116, 116, 117, 117, 117, 117, 117, 
                                                                                                             117, 117, 116, 116, 117, 117, 117, 117, 117, 117, 117, 117, 117, 
                                                                                                             117, 117, 118, 118, 118, 118, 117, 118, 117, 117, 117, 117, 117, 
                                                                                                             117, 118, 116, 116, 116, 116, 116, 116, 116, 117, 117, 118, 118, 
                                                                                                             118, 118, 118, 119, 120, 120, 119, 119, 120, 120, 121, 121, 122, 
                                                                                                             124, 124, 122, 123, 124, 123, 123, 123, 123, 123, 124, 124, 126, 
                                                                                                             126, 126, 126, 126, 125, 125, 126, 127, 126, 126, 125, 126, 126, 
                                                                                                             126, 128, 128, 128, 130, 133, 131, 133, 134, 134, 134, 136, 136, 
                                                                                                             136, 135, 135, 135, 136, 136, 136, 136, 135, 135, 135, 135, 130, 
                                                                                                             129, 129, 130, 131, 136, 138, 155, 157, 161, 170, 174, 168, 165, 
                                                                                                             169, 171, 181, 184, 182, 179, 181, 179, 175, 177, 177, 174, 170, 
                                                                                                             174, 173, 178, 173, 178, 179, 182, 184, 184, 180, 181, 182, 182, 
                                                                                                             184, 184, 188, 195, 198, 220, 255, 275, 350, 310, 315, 320, 320, 
                                                                                                             316, 300, 310, 310, 320, 317, 313, 312, 310, 297, 285, 285, 286, 
                                                                                                             288, 315, 328, 338, 344, 345, 352, 352, 342, 335, 343, 340, 342, 
                                                                                                             339, 337, 336, 336, 342, 347, 352, 352, 351, 352, 352, 351, 352, 
                                                                                                             352, 355, 375, 400, 452, 487, 476, 475, 473, 485, 500, 530, 595, 
                                                                                                             720, 720, 770, 750, 770, 750, 735, 740, 745, 735, 700, 700, 750, 
                                                                                                             760, 755, 755, 760, 760, 765, 950, 950, 950, 875, 875, 875, 880, 
                                                                                                             880, 880, 900, 900, 900, 880, 880, 890, 895, 890, 880, 870, 870, 
                                                                                                             870, 870, 870, 860, 860, 860, 860, 850, 840, 810, 820, 810, 810, 
                                                                                                             805, 810, 805, 820, 815, 820, 805, 790, 800, 780, 760, 765, 750, 
                                                                                                             740, 820, 810, 800, 800, 775, 750, 810, 750, 740, 700, 705, 660, 
                                                                                                             630, 640, 595, 590, 570, 565, 535, 440, 400, 410, 400, 405, 390, 
                                                                                                             370, 300, 300, 180, 200, 310, 290, 260, 260, 275, 260, 270, 265, 
                                                                                                             255, 250, 210, 210, 200, 195, 210, 215, 240, 240, 220, 220, 220, 
                                                                                                             220, 210, 212, 208, 220, 210, 212, 208, 220, 215, 220, 214, 214, 
                                                                                                             213, 212, 210, 210, 195, 195, 160, 160, 175, 205, 210, 208, 197, 
                                                                                                             181, 185)), row.names = c(NA, 393L), class = "data.frame")

ggplot(df1, aes(Date, value)) +
  geom_line() +
  scale_x_date(date_labels=paste(c(rep(" ",11), "%b"), collapse=""), 
               date_breaks="month", expand=c(0,0)) +
  facet_grid(~ year(Date), space="free_x", scales="free_x", switch="x") +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA,colour="grey50"),
        panel.spacing=unit(0,"cm"), 
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())


Solution

  • For your first question. The issue is that last obs. in df1 for year 2019 is December 1st, hence that's the max. value displayed on the x axis for this panel. In contrast for your df1 it's December 31st. As a consequence the axis labels overlap and the lines do not connect. Actually the lines do not connect either in your second plot. It only looks like this.

    One fix for both issues would be to add a helper observation to your df dataset which extends the scale and at the same time "connects" the line to the year 2020:

    Note: While adding spaces might work an alternative approach to shift your labels would be to adjust the horizontal alignment.

    library(tidyverse)
    
    df <- add_row(df, date = as.Date("2019-12-31"), year = 2019, month = "Dec", sales = 100)
    
    ggplot(df, aes(x = date, y = sales)) +
      geom_line() +
      scale_x_date(
        date_labels = "%b",
        date_breaks = "month", 
        expand = c(0, 0)
      ) +
      facet_grid(~ year(date), space = "free_x", scales = "free_x", switch = "x") +
      theme_bw() +
      theme(
        strip.placement = "outside",
        strip.background = element_rect(fill = NA, colour = "grey50"),
        panel.spacing = unit(0, "lines"),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        panel.margin.x = unit(0, "cm"),
        axis.text.x = element_text(hjust = -.1)
      )
    

    enter image description here

    For your second question. To get rid of the border and the line between years set the color for the strip background to NA:

    ggplot(df1, aes(Date, value)) +
      geom_line() +
      scale_x_date(
        date_labels = "%b",
        date_breaks = "month", expand = c(0, 0)
      ) +
      facet_grid(~ year(Date), space = "free_x", scales = "free_x", switch = "x") +
      theme_bw() +
      theme(
        strip.placement = "outside",
        strip.background = element_rect(fill = NA, colour = NA),
        panel.spacing = unit(0, "cm"),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.text.x = element_text(hjust = -.1)
      )
    

    enter image description here