Home > front end >  ggplot2: Can I fix the absolute distance between two values on an axis?
ggplot2: Can I fix the absolute distance between two values on an axis?

Time:02-18

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!

CodePudding user response:

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

CodePudding user response:

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.)

  • Related