Source data : http://data.giss.nasa.gov/gistemp/
library(reshape2) # melt
library(ggplot2)
library(mgcv) # for ggplot smoothing
## Loading required package: nlme
## This is mgcv 1.8-7. For overview type 'help("mgcv-package")'.
# gistemp estimate for absolute global mean for 1951-1980 is 14.0 deg-C (cf. file)
agm <- 14.0
gistemp <- head(
read.table("http://data.giss.nasa.gov/gistemp/tabledata_v3/GLB.Ts+dSST.txt",
skip=7, na.strings=c("****", "***"), fill=TRUE, blank.lines.skip=TRUE, stringsAsFactors=FALSE, header=TRUE),
-9)
# Remove rows with garbage strings
gistemp <- gistemp[gistemp$Year != "Year",]
# remove unused last 7 columns
gistemp <- gistemp[, names(gistemp) %in% head(colnames(gistemp), -7)]
# rename columns : month name to month number
colnames(gistemp) <- c("Year", values=seq(1:12))
# long format
gistemp <- melt(gistemp, id.vars="Year", variable.name="month", value.name="tm")
gistemp$Year <- as.numeric(gistemp$Year)
# use Celsius degrees (and not variation from 51-80 mean)
gistemp$tm <- (as.numeric(gistemp$tm) / 100) + agm
# create a date column
gistemp$date <- as.Date(paste0(gistemp$Year, "-", gistemp$month, "-01"))
ggplot(gistemp, aes(date, tm)) +
geom_line(color="#cccccc") +
geom_point(color="darkgrey") +
stat_smooth() +
xlab("Year") +
ylab(expression("Temperature ("*degree*"C)")) +
ggtitle("Global combined land-surface air and sea-surface water monthly temperature (GISTEMP)")
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
## Warning: Removed 5 rows containing missing values (stat_smooth).
## Warning: Removed 5 rows containing missing values (geom_path).
## Warning: Removed 5 rows containing missing values (geom_point).
# Use last complete year
maxdate = max(gistemp$date)
lcy <- as.numeric(ifelse(
is.na(gistemp[which(gistemp$date == maxdate),]["tm"]),
as.numeric(format(maxdate, "%Y")) - 1,
as.numeric(format(maxdate, "%Y"))))
ggplot(subset(gistemp, gistemp$Year < lcy), aes(Year, tm)) +
stat_summary(fun.data="mean_cl_boot", color="#999999") +
stat_smooth() +
xlab("Year") +
ylab(expression("Temperature ("*degree*"C)")) +
ggtitle("Global combined land-surface air and sea-surface water yearly temperature (GISTEMP)")
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.