Created
August 22, 2017 10:57
-
-
Save cavedave/6dfea9ef67995edc3be15265bb78e8d0 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
setwd("/Users/davidcurran/Documents/monthlyEngland") | |
readLines("cetml1659on.dat", | |
n=10) | |
#Start of borrowed bit | |
## from https://gist.github.com/gavinsimpson/b52f6d375f57d539818b | |
CET <- url("http://www.metoffice.gov.uk/hadobs/hadcet/cetml1659on.dat") | |
cet <- read.table("cetml1659on.dat", sep = "", skip = 6, header = TRUE, | |
fill = TRUE, na.string = c(-99.99, -99.9)) | |
names(cet) <- c(month.abb, "Annual") ## fix up df names | |
## remove last row of incomplete data | |
cet <- cet[-nrow(cet), ] | |
## get rid of the annual too - store for plotting | |
rn <- as.numeric(rownames(cet)) | |
Years <- rn[1]:rn[length(rn)] | |
annCET <- data.frame(Temperature = cet[, ncol(cet)], | |
Year = Years) | |
cet <- cet[, -ncol(cet)] | |
## stack the data | |
cet <- stack(cet)[,2:1] | |
names(cet) <- c("Month","Temperature") | |
## add in Year and nMonth for numeric month and a proper Date class | |
cet <- transform(cet, Year = (Year <- rep(Years, times = 12)), | |
nMonth = rep(1:12, each = length(Years)), | |
Date = as.Date(paste(Year, Month, "15", sep = "-"), | |
format = "%Y-%b-%d")) | |
## sort into temporal order | |
cet <- cet[with(cet, order(Date)), ] | |
## Add in a Time variable | |
cet <- transform(cet, Time = as.numeric(Date) / 1000) | |
head(cet) | |
#end of borrowed bit | |
library(dplyr) | |
jan<- cet %>% | |
group_by(Month)%>% | |
summarise(avg_temp = mean(Temperature), | |
min_temp = min(Temperature), | |
max_temp = max(Temperature), | |
total = n()) | |
jan | |
Month avg_temp min_temp max_temp total | |
<fctr> <dbl> <dbl> <dbl> <int> | |
1 Apr 7.935196 4.7 11.8 358 | |
2 Aug 15.638827 12.9 19.2 358 | |
3 Dec 4.103352 -0.8 9.7 358 | |
4 Feb 3.871788 -1.9 7.9 358 | |
5 Jan 3.258380 -3.1 7.5 358 | |
6 Jul 15.973743 13.4 19.7 358 | |
7 Jun 14.325698 11.5 18.2 358 | |
8 Mar 5.334078 1.0 9.2 358 | |
9 May 11.222067 8.5 15.1 358 | |
10 Nov 6.072905 2.3 10.1 358 | |
11 Oct 9.718436 5.3 13.3 358 | |
12 Sep 13.345251 10.5 16.8 358 | |
#now 2017 | |
cet17 <- read.table("cet_mean_2017.dat", sep = "", skip = 4, header = FALSE,fill = TRUE, na.string = c(-32768,-32768)) | |
names(cet17)[1] <- "day" | |
names(cet17)[2] <- "Jan" | |
names(cet17)[3] <- "Feb" | |
names(cet17)[4] <- "Mar" | |
names(cet17)[5] <- "Apr" | |
names(cet17)[6] <- "May" | |
names(cet17)[7] <- "Jun" | |
names(cet17)[8] <- "Jul" | |
cet17[9] <- NULL | |
cet17[10] <- NULL | |
cet17[11] <- NULL | |
cet17[12] <- NULL | |
cet17[13] <- NULL | |
summary(cet17) | |
#now graph | |
p<-ggplot(data=jan, aes(x=Month, y=avg_temp))+ | |
scale_x_discrete(limits = month.abb)+ | |
geom_line() + | |
theme_tufte()+ | |
geom_point() | |
#jan | |
p<-p + annotate("text", x = 1, y = -3.1, label = "1795",color='blue') | |
p<-p + annotate("text", x = 1, y = 7.5, label = "1916",color='red') | |
p<-p + annotate("text", x = 1, y = 3.95, label = "*",color='dark green',size=5) | |
#feb | |
p<-p + annotate("text", x = 2, y = -1.9, label = "1947",color='blue') | |
p<-p + annotate("text", x = 2, y = 7.9, label = "1779",color='red') | |
p<-p + annotate("text", x = 2, y = 6.09, label = "*",color='dark green',size=5) | |
#mar | |
p<-p + annotate("text", x = 3, y = 1, label = "1674",color='blue') | |
p<-p + annotate("text", x = 3, y = 9.2, label = "1957",color='red') | |
p<-p + annotate("text", x = 3, y = 8.6, label = "*",color='dark green',size=5) | |
#apr | |
p<-p + annotate("text", x = 4, y = 4.7, label = "1837",color='blue') | |
p<-p + annotate("text", x = 4, y = 11.8, label = "2011",color='red') | |
p<-p + annotate("text", x = 4, y = 8.6, label = "*",color='dark green',size=5) | |
#May | |
p<-p + annotate("text", x = 5, y = 8.5, label = "1698",color='blue') | |
p<-p + annotate("text", x = 5, y = 15.1, label = "1833",color='red') | |
p<-p + annotate("text", x = 5, y = 13.2, label = "*",color='dark green',size=5) | |
#June | |
p<-p + annotate("text", x = 6, y = 11.5, label = "1675",color='blue') | |
p<-p + annotate("text", x = 6, y = 18.2, label = "1846",color='red') | |
p<-p + annotate("text", x = 6, y = 16.04, label = "*",color='dark green',size=5) | |
#Jul | |
p<-p + annotate("text", x = 7, y = 13.4, label = "1816",color='blue') | |
p<-p + annotate("text", x = 7, y = 19.7, label = "2006",color='red') | |
p<-p + annotate("text", x = 7, y = 16.77, label = "*",color='dark green',size=5) | |
#Aug | |
p<-p + annotate("text", x = 8, y = 12.9, label = "1912",color='blue') | |
p<-p + annotate("text", x = 8, y = 19.2, label = "1995",color='red') | |
p<-p + annotate("text", x = 8, y = 17, label = "*",color='dark green',size=5) | |
#Sep | |
p<-p + annotate("text", x = 9, y = 10.5, label = "1807",color='blue') | |
p<-p + annotate("text", x = 9, y = 16.8, label = "2006",color='red') | |
p<-p + annotate("text", x = 9, y = 16, label = "*",color='dark green',size=5) | |
#Oct | |
p<-p + annotate("text", x = 10, y = 5.3, label = "1740",color='blue') | |
p<-p + annotate("text", x = 10, y = 13.3, label = "2001",color='red') | |
p<-p + annotate("text", x = 10, y = 10.9, label = "*",color='dark green',size=5) | |
#Nov | |
p<-p + annotate("text", x = 11, y = 2.3, label = "1782",color='blue') | |
p<-p + annotate("text", x = 11, y = 10.1, label = "1994",color='red') | |
p<-p + annotate("text", x = 11, y = 5.6, label = "*",color='dark green',size=5) | |
#Dec | |
p<-p + annotate("text", x = 12, y = -0.8, label = "1890",color='blue') | |
p<-p + annotate("text", x = 12, y = 9.7, label = "2015",color='red') | |
p<-p + annotate("text", x = 12, y = 6, label = "*",color='dark green',size=5) | |
p<-p + annotate("text", x = 2, y = 15.4, label = "* Temp in last year",color='dark green') | |
#p<-p + annotate("text", x = 2, y = 15.4, label = " Average Temp",color='black') | |
p<-p + labs(y = "Average Temperature") | |
p<-p + labs(title = "Temperature in England per month since 1659", subtitle = "Average, highest, lowest and last year") | |
#may | |
#p<-p + annotate("text", x = 10, y = -4, label = "HadCET data #graph by @iamreddave") | |
ggsave("temperatur2017.jpg") | |
p | |
#low | |
janm<-filter(cet, Temperature == -3.1 & Month=='Jan') | |
#high | |
janx<-filter(cet, Temperature == 7.5 & Month=='Jan') | |
#2016 | |
jan2<-filter(cet, Year=='2016'& Month=='Jan') | |
print(jan2) | |
#low | |
febn<-filter(cet, Temperature == -1.9 & Month=='Feb') | |
#high | |
febx<-filter(cet, Temperature == 7.9 & Month=='Feb') | |
#2016 | |
feb2<-filter(cet, Year=='2016'& Month=='Feb') | |
print(feb2) | |
#low | |
marn<-filter(cet, Temperature == 1 & Month=='Mar') | |
#high | |
marx<-filter(cet, Temperature == 9.2 & Month=='Mar') | |
#2016 | |
mar<-filter(cet, Year=='2016'& Month=='Mar') | |
print(mar) | |
#low | |
aprn<-filter(cet, Temperature == 4.7 & Month=='Apr') | |
#high | |
aprx<-filter(cet, Temperature == 11.8 & Month=='Apr') | |
#2016 | |
apr<-filter(cet, Year=='2016'& Month=='Apr') | |
print(aprn) | |
#low | |
mayn<-filter(cet, Temperature == 8.5 & Month=='May') | |
#high | |
mayx<-filter(cet, Temperature == 15.1 & Month=='May') | |
#2016 | |
may<-filter(cet, Year=='2016'& Month=='May') | |
print(may) | |
#low | |
junn<-filter(cet, Temperature == 11.5 & Month=='Jun') | |
#high | |
junx<-filter(cet, Temperature == 18.2 & Month=='Jun') | |
#2016 | |
jun<-filter(cet, Year=='2016'& Month=='Jun') | |
print(jun) | |
#low | |
juln<-filter(cet, Temperature == 13.4 & Month=='Jul') | |
#high | |
julx<-filter(cet, Temperature == 19.7 & Month=='Jul') | |
#2016 | |
jul<-filter(cet, Year=='2016'& Month=='Jul') | |
print(jul) | |
#low | |
augn<-filter(cet, Temperature == 12.9 & Month=='Aug') | |
#high | |
augx<-filter(cet, Temperature == 19.2 & Month=='Aug') | |
#2016 | |
aug<-filter(cet, Year=='2016'& Month=='Aug') | |
print(aug) | |
#low | |
Sepn<-filter(cet, Temperature == 10.5 & Month=='Sep') | |
#high | |
Sepx<-filter(cet, Temperature == 16.8 & Month=='Sep') | |
#2016 | |
Sep<-filter(cet, Year=='2016'& Month=='Sep') | |
print(Sep) | |
#low | |
Octn<-filter(cet, Temperature == 5.3 & Month=='Oct') | |
#high | |
Octx<-filter(cet, Temperature == 13.3 & Month=='Oct') | |
#2016 | |
Oct<-filter(cet, Year=='2016'& Month=='Oct') | |
print(Oct) | |
#low | |
Novn<-filter(cet, Temperature == 2.3 & Month=='Nov') | |
#high | |
Novx<-filter(cet, Temperature == 10.1 & Month=='Nov') | |
#2016 | |
Nov<-filter(cet, Year=='2016'& Month=='Nov') | |
print(Nov) | |
#low | |
Decn<-filter(cet, Temperature == -0.8 & Month=='Dec') | |
#high | |
Decx<-filter(cet, Temperature == 9.7 & Month=='Dec') | |
#2016 | |
Dec<-filter(cet, Year=='2016'& Month=='Dec') | |
print(Dec) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment