Home > OS >  Removing dates with no data between two years
Removing dates with no data between two years

Time:06-22

I have three months (March-May) of mean temperature data for years 2021 and 2022, but when I try to plot it in ggplot and separate it by years it still includes June 2021-February 2022 leaving a large blank in the middle of the graph. My current code looks like this:

climate %>% 
  ggplot(aes(x = Date, y = `Mean Temp (C)`))  
  geom_line(aes(color = Year))  
  facet_grid(. ~ Year)

which gives this:

enter image description here

How can I break the x-axis so that it ends at the end of May 2021 and starts again at the beginning of March 2022? I have tried (and stupidly deleted) various versions of scale_x_break, scale_x_date, and xlim but can't seem to get it to work.

Side note: is there a reason it displays year that way in the legend? The year column only has "2021" or "2022".

Any help is appreciated thanks!

CodePudding user response:

You can use scales="free_x" within facet_grid:

library(tidyverse)

climate %>% 
  ggplot(aes(x = Date, y = `Mean Temp (C)`))  
  geom_line(aes(color = Year))  
  facet_grid(. ~ Year, scales="free_x")

Output

enter image description here

Data

climate <- structure(list(Date = structure(c(14669, 14670, 14671, 14672, 
14673, 14674, 14675, 14676, 14677, 14678, 14679, 14680, 14681, 
14682, 14683, 14684, 14685, 14686, 14687, 14688, 14689, 14690, 
14691, 14692, 14693, 14694, 14695, 14696, 14697, 14698, 14699, 
14700, 14701, 14702, 14703, 14704, 14705, 14706, 14707, 14708, 
14709, 14710, 14711, 14712, 14713, 14714, 14715, 14716, 14717, 
14718, 14719, 14720, 14721, 14722, 14723, 14724, 14725, 14726, 
14727, 14728, 14729, 14730, 14731, 14732, 14733, 14734, 14735, 
14736, 14737, 14738, 14739, 14740, 14741, 14742, 14743, 14744, 
14745, 14746, 14747, 14748, 14749, 14750, 14751, 14752, 14753, 
14754, 14755, 14756, 14757, 14758, 14759, 14760, 15034, 15035, 
15036, 15037, 15038, 15039, 15040, 15041, 15042, 15043, 15044, 
15045, 15046, 15047, 15048, 15049, 15050, 15051, 15052, 15053, 
15054, 15055, 15056, 15057, 15058, 15059, 15060, 15061, 15062, 
15063, 15064, 15065, 15066, 15067, 15068, 15069, 15070, 15071, 
15072, 15073, 15074, 15075, 15076, 15077, 15078, 15079, 15080, 
15081, 15082, 15083, 15084, 15085, 15086, 15087, 15088, 15089, 
15090, 15091, 15092, 15093, 15094, 15095, 15096, 15097, 15098, 
15099, 15100, 15101, 15102, 15103, 15104, 15105, 15106, 15107, 
15108, 15109, 15110, 15111, 15112, 15113, 15114, 15115, 15116, 
15117, 15118, 15119, 15120, 15121, 15122, 15123, 15124, 15125
), class = "Date"), `Mean Temp (C)` = c(49.7, 51.2, 51.3, 48.7, 
40.1, 47.8, 54.6, 38.4, 53.1, 42.6, 48.9, 43, 54.1, 47, 52.1, 
46, 44.1, 42.5, 44.2, 48.2, 56.9, 52.8, 46.8, 51.2, 45, 46.9, 
50.8, 63.4, 51.1, 50.5, 38.5, 55.9, 56.2, 41.9, 50, 45.8, 50.6, 
47.5, 55.1, 51.1, 50.4, 51.9, 40.9, 44.1, 56.8, 51, 52, 43, 60.9, 
47.3, 52.4, 48.7, 54.6, 45.4, 54.9, 55, 46, 41.7, 52, 38.3, 53.7, 
53.8, 53.9, 48.3, 59.1, 49.3, 45.1, 48.1, 43.7, 43.6, 51, 50.2, 
45.5, 48.9, 49.2, 48.5, 58.8, 47, 53.4, 48.3, 44.7, 45.3, 50.8, 
47.1, 50.8, 51.7, 53.7, 44.9, 56.8, 40.4, 40.4, 55.7, 44.8, 56.1, 
43.5, 44.1, 44.3, 50.5, 55.5, 42.2, 48, 50.4, 42.1, 48.7, 49, 
55.2, 53.3, 43.5, 44.3, 46.3, 39.4, 44.2, 54, 48, 53.8, 54, 46.2, 
47.7, 59.5, 48.3, 51.1, 55.7, 49.4, 50.7, 43.8, 60, 46.1, 44.1, 
47.2, 58.8, 41.1, 49, 43.8, 44.8, 50.1, 52.4, 58.3, 48, 52.2, 
36.2, 50.7, 58.2, 52.5, 49.5, 49.2, 47.1, 52.4, 52.2, 57.4, 39.4, 
50.1, 47, 48.6, 48.6, 46.4, 51.5, 46.4, 42.2, 55.4, 48.7, 49, 
49.8, 44.2, 53.1, 51.6, 43.2, 51.4, 48.7, 54.1, 48.8, 51.4, 56.7, 
59, 44.6, 49.4, 43.2, 52.4, 51.5, 47.2, 45.2, 61.8, 49.9, 51.5, 
45.7), Year = structure(c(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, 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, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 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, 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("2010", 
"2011"), class = "factor")), row.names = c(NA, -184L), class = "data.frame")
  • Related