'ggplot2: Can I fix the absolute distance between two values on an axis?

My problem seems quite basic, but I couldn't find any relevant answer. I want to create line plots with the date on the x axis. The y axis will be Covid statistics (deaths, hospitalizations, you name it). I want to create a separate plot for the different waves of the pandemic which means that my charts cover different times. My problem is that R fixes the plot to the same size and thus the lines for the shorter time period are skewed in comparison to those of the longer time period. Ideally, I would want 1 month on the x axis to be fixed to a certain number of px or mm. But I can't find out how. My best idea so far is to assign both plots a different total width, but that doesn't give me an optimal result either. Here's a reproducible example with a built-in dataset to explain:

library(ggplot2)
library(dplyr)
economics_1967 <- economics %>%
  filter(date<"1968-01-01")
economics_1968 <- economics %>%
  filter(date<"1969-01-01"&date>"1967-12-31")
#data is only available for six months in 1967, but for 12 in 1968
exampleplot1 <- ggplot(economics_1967)+
  geom_line(aes(date, unemploy))+
  scale_x_date(date_breaks="1 month", date_labels="%b")
#possible: ggsave("exampleplot1.png", width=2, height=1)

exampleplot2 <- ggplot(economics_1968)+
  geom_line(aes(date, unemploy))+
  scale_x_date(date_breaks="1 month", date_labels="%b")
ggsave("exampleplot2.png", width=4, height=1)
#possible: ggsave("exampleplot1.png", width=2, height=1)

Thank you!

EDIT: Thanks for the suggestions! Facet wrap would be a good idea but in the end I decided to just plot the whole time in one case. The background is that I classified countries differently for their policies in different times, so that's why I wanted to have a clear break in the visualization, but I just put a vertical line in there.



Solution 1:[1]

facet_grid is one approach, if you don't mind showing the two charts together.

library(dplyr); library(ggplot2)
bind_rows(e1967 = economics_1967,
          e1968 = economics_1968, .id="source") %>%
  ggplot(aes(date, unemploy)) +
  geom_line() +
  scale_x_date(date_breaks="1 month", date_labels="%b") +
  facet_grid(~source, scales = "free_x", space = "free_x")

enter image description here

Solution 2:[2]

I like @Jon Spring's solution a lot. I want to present it a tad differently --to show that facet() usually operates on a single dataset that has one existing variable used to facet.

econ_subset <- 
  economics %>%
  dplyr::filter(dplyr::between(date, as.Date("1967-09-01"), as.Date("1968-12-31"))) %>%
  dplyr::mutate(
    year = lubridate::year(date) # Used below to facet
  )

ggplot(econ_subset, aes(date, unemploy)) +
  geom_line() +
  scale_x_date(date_breaks="1 month", date_labels="%b") +
  facet_grid(~year, scales = "free_x", space = "free_x")

enter image description here

(In Jon's solution, bind_rows() is used to stack the two separate datasets back together.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon Spring
Solution 2 wibeasley