Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thoughtfulbloke/dd4f12a77bf06741eb7ad662de991db8 to your computer and use it in GitHub Desktop.
Save thoughtfulbloke/dd4f12a77bf06741eb7ad662de991db8 to your computer and use it in GitHub Desktop.
Code for CO2 temp graph
co2 = read.table("ftp://aftp.cmdl.noaa.gov/products/trends/co2/co2_annmean_mlo.txt")
temp = read.table("https://climate.nasa.gov/system/internal_resources/details/original/647_Global_Temperature_Data_File.txt")
names(co2) = c("year", "co2", "unc")
names(temp) = c("year", "raw_temp", "smoothed_temp")
dta <- merge(co2,temp, by="year")
plot(dta$co2, dta$raw_temp, pch=13, cex=0.5, frame.plot=FALSE, xlab="co2", ylab="temperature",
main="Global CO2 and Temperature 1959 to 2016")
lm_model <- lm(raw_temp ~ co2, data=dta)
abline(lm_model, col="blue", lwd=2)
annaul_var <- data.frame(co2=dta$co2, temp=dta$raw_temp, res=lm_model$residuals)
apply(annaul_var,1, function(x){lines(c(x[1],x[1]), c(x[2],x[2]-x[3]), lty=2, col="red")})
legend("topleft", legend=c("What we are talking about- the major long term trend",
"What you want to talk about-\nthe variation of individual years to the trend"),
lwd=c(2,1), lty=c(1,2), col=c("blue","red"), cex=0.75, bty="n", y.intersp=1.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment