Created
December 12, 2017 23:05
-
-
Save cavedave/49e8fdf673874eac8ef98bd4f2869a5d to your computer and use it in GitHub Desktop.
Animation of rising CO2 levels
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
#read in data to look like | |
#Year,Month,Date,decimal,average,interpolated,trend | |
#1 1958 3 1958.208 315.71 315.71 314.62 -1 | |
#2 1958 4 1958.292 317.45 317.45 315.29 -1 | |
library(dplyr) | |
library(tidyr) | |
library(ggplot2) | |
library(animation) | |
#remove columns that wont be used | |
df <- subset(co2, select = -c(Date,decimal,interpolated,trend) | |
mo <- months(seq(as.Date("1910/1/1"), as.Date("1911/1/1"), "months")) | |
mo <- gsub("(^...).*", "\\1", mo) | |
#4. make animation | |
saveGIF({ | |
for(i in 1958:2017){ | |
print(ggplot(df %>% filter(Year <= i), | |
# aes(x=Month, y=average, color=Year, group=Year)) + | |
aes(x=Month, y=average, color=Year,group=Year)) + | |
#scale_color_viridis(discrete=TRUE) + | |
geom_line()+ | |
scale_color_gradient(low="blue", high="black", limits=c(1958, 2017), guide="none")+ | |
annotate(x=1.3, y=270, geom="text", label=i, size = 8) + | |
annotate(x=1.3, y=285, geom="text", label = "preindustrial",parse = TRUE, size = 4) + | |
ggtitle('World CO2 levels ppm')+ | |
scale_x_continuous(labels=mo, breaks=1:13) + | |
#scale_y_continuous(breaks=0:400) + | |
#scale_y_continuous(breaks=0:400)+#,limits=(0:500) | |
#ylim(0, 500)+ | |
expand_limits(x=c(1,12),y=c(270, 420))+ | |
scale_y_continuous("CO2 level",breaks=c(270, 300, 400, 420), | |
labels=c("270", "300", "400", "420"))+ | |
theme_classic() | |
+ | |
annotate(x=9, y=270, geom="text", label = "Chart by @iamreddave | Data:esrl.noaa.gov", size = 4) | |
)} | |
}, interval=0.25,ani.width = 600, ani.height = 600) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment