Created
August 23, 2017 13:02
-
-
Save robwschlegel/ea59a11e10101da07550cff6e764f9dd to your computer and use it in GitHub Desktop.
This file contains 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
## Function that creates a polar plot | |
polar.plot <- function(df, i){ | |
# Add bridges for polar coordinates | |
years <- unique(df$year)[1:i] | |
df2 <- filter(df, year %in% years) | |
bridges <- df2[df2$month == 'Jan',] | |
bridges$year <- bridges$year - 1 | |
if(nrow(bridges) == 0){ | |
bridges <- data.frame(site = df2$site[1], year = min(df2$year), month = NA, anom = NA) | |
} else { | |
bridges$month <- NA | |
} | |
blanks <- data.frame(site = df2$site[1], expand.grid(year = min(df2$year)-1, month = month.abb), anom = NA) | |
# Polar plot | |
pp <- ggplot(data = rbind(blanks, df2, bridges), aes(x = month, y = anom, group = year)) + | |
# Circular black background | |
geom_rect(colour = "black", fill = "black", aes(xmin = "Jan", xmax = NA, | |
ymin = min(df$anom, na.rm = T), ymax = max(df$anom, na.rm = T))) + | |
# ymin = min(df$anom, na.rm = T), ymax = 3)) + | |
# Anomaly threshold labels | |
geom_hline(aes(yintercept = 1.0), colour = "red") + | |
geom_label(aes(x = "Jan", y = 1.0, label = "1.0°C"), | |
colour = "red", fill = "black", size = 3) + | |
geom_hline(aes(yintercept = 2.0), colour = "red") + | |
geom_label(aes(x = "Jan", y = 2.0, label = "2.0°C"), | |
colour = "red", fill = "black", size = 3) + | |
geom_hline(aes(yintercept = 3.0), colour = "red") + | |
geom_label(aes(x = "Jan", y = 3.0, label = "3.0°C"), | |
colour = "red", fill = "black", size = 3) + | |
geom_hline(aes(yintercept = 4.0), colour = "red") + | |
geom_label(aes(x = "Jan", y = 4.0, label = "4.0°C"), | |
colour = "red", fill = "black", size = 3) + | |
# Temperature spiral | |
geom_path(aes(colour = anom), show.legend = F) + | |
geom_point(data = df2[i,], aes(colour = anom), show.legend = F) + | |
# Scale corrections | |
scale_colour_viridis(limits = c(min(df$anom, na.rm = T), max(df$anom, na.rm = T))) + | |
scale_x_discrete(expand = c(0,0), breaks = month.abb) + | |
scale_y_continuous(expand = c(0,0), | |
limits = c(min(df$anom, na.rm = T), max(df$anom, na.rm = T))) + | |
# Year label | |
geom_text(aes(x = "Jan", y = min(df$anom, na.rm = T), label = max(df2$year, na.rm = T)), | |
colour = "ivory", size = 8) + | |
# Additional tweaks | |
ggtitle(paste0(df$site[1]," temperature change (",min(df$year),"-",max(df$year),")")) + | |
coord_polar() + | |
theme(panel.background = element_rect(fill = "grey20"), | |
plot.background = element_rect(fill = "grey20"), | |
panel.grid.major = element_blank(), | |
panel.grid.minor = element_blank(), | |
# axis.text.x = element_text(colour = "ivory"), | |
axis.text.x = element_text(colour = "ivory", angle = | |
(360/(2*pi)*rev(seq(pi/12, 2*pi-pi/12, len = 12)))+15, | |
size = 12), | |
axis.text.y = element_blank(), | |
axis.title = element_blank(), | |
axis.ticks = element_blank(), | |
axis.ticks.length = unit(0, "cm"), | |
plot.title = element_text(hjust = 0.5, colour = "ivory", size = 15)) | |
print(pp) | |
} | |
## Create animation of polar plots | |
animate.polar.plot <- function(df) { | |
lapply(seq(1,length(unique(df$year))), function(i) { | |
polar.plot(df = df, i = i) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment