Created
June 5, 2017 00:23
-
-
Save joranE/931b58ed9b11bcb8b0775a99614c86f4 to your computer and use it in GitHub Desktop.
Plotting irregular times series with predictable gaps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dat <- structure(list(date = structure(c(14645, 14590, 14577, 14569, | |
14289, 14261, 14226, 14219, 14213, 14205, 14295, 14655, 14660, | |
14933, 14940, 14941, 14954, 14961, 14974, 14975, 14977, 14980, | |
14982, 14983, 15024, 15032, 15034, 15045, 15051, 15052, 15053, | |
15297, 15304, 15305, 15318, 15325, 15337, 15338, 15340, 15342, | |
15344, 15346, 15347, 15361, 15374, 15375, 15381, 15402, 15409, | |
15668, 15675, 15687, 15690, 15703, 15704, 15708, 15709, 15710, | |
15711, 15753, 15759, 15767, 15774, 15780, 16039, 16040, 16046, | |
16102, 16110, 16115, 16124, 16487, 16491, 16502, 16767, 16768, | |
16774, 16781, 16789), class = "Date"), y = c(-2.4215, -2.7139, | |
-3.4126, -2.4506, -2.277, 0.1253, -0.9912, 0.0673, -2.5237, -1.7491, | |
-5.4418, 2.0619, 6.631, -3.3748, -4.3086, -2.6573, -1.7799, -1.9859, | |
1.8146, -0.7082, -0.7949, 0.2595, 10.2503, -2.2439, 1.4828, -1.061, | |
-1.8546, -0.5535, 0.5664, -0.5346, 2.2731, 0.5518, -2.5637, 0.512, | |
0.9785, 1.6282, 1.261, -1.7903, 0.4518, 1.1885, 0.0195, 5.9399, | |
0.6011, -1.558, -0.2032, -0.8846, 0.118, 1.7331, -0.5502, -0.9086, | |
-1.3498, -3.0417, -4.1688, -0.8954, 1.0585, 0.9385, 2.5769, 1.2266, | |
5.5859, 0.4341, 0.9447, 1.9321, -1.3592, 1.1439, -0.1818, 0.0847, | |
5.4002, 3.1371, 4.2006, 1.1091, 10.6764, 5.8082, 3.0979, 4.0353, | |
4.0156, 2.4512, 6.5072, 4.4321, 6.29)), class = "data.frame", row.names = c(NA, | |
-79L), .Names = c("date", "y")) | |
#Base graphics | |
plot(dat) | |
#ggplot2 | |
library(ggplot2) | |
ggplot(dat,aes(x = date,y = y)) + | |
geom_point() | |
#Generally the data will only appear for Nov-Apr, never May-Oct. | |
# Leaving them in often makes the data squashed together and hard to see. | |
# 1) Leave gaps in or not? | |
# 2) Are there existing ggplot2 tools for dealing with axes like these? | |
# 3) Ideas? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My idea was to take the timepoints that you have and space them out using a factor scale, but this really won't work because you have daily data and you would need a vector with every date in between your months. You can do this, but then labeling gets pretty hard too. I know Hadley isn't big on broken axes, but this is an example where it would be useful.
The alternative is faceting, but direct comparisons are harder and your "seasons" aren't Year, they span years. I think this means you need to copy the year (really season) to a column, translate your dates to a single season, then facet by the copied season with a defined date range of a single season. You could color by season to see the data directly on top of each other, or facet_grid( . ~ Season) or facet_grid(Season ~.)
library(tidyverse)
library(lubridate)
dat %>%
mutate(year = year(date), month = month(date), yday = yday(date)) %>%
mutate(
season = if_else(month < 6, year-1, year),
ydayadj = if_else(yday > 275, yday - 365, yday)
) %>%
ggplot(., aes(x = yday, y = y)) +
geom_point() +
facet_grid(.~season)