library(data.table)
library(ggplot2)
library(dplyr)
library(ggmap)
library(maps)
library(maptools)
library(animation)
world <- map_data(“world”)
####create a NA data.frame
total<-data.frame(matrix(NA, nrow = nrow(world), ncol = 271))
####change colnames as year
colnames(total)<-c(1743:2013)
####combine world and total
total<-cbind(world,total)
tem<-fread(“./GlobalLandTemperaturesByCountry.csv”)
tem<-as.data.frame(tem)
tem\(Country<-as.factor(tem\)Country)
####remove Country name which equal to “3and”
tem<-tem[tem\(Country!=levels(tem\)Country)[1],]
####splite Year from df
colnames(tem)<-c(“dt”,“tem”,“temun”,“Country”)
tem\(year<-as.factor(year(as.POSIXct(tem\)dt,format=“%Y-%m-%d”)))
for(i in 1:ncol(total)){
if(is.logical(total[,i])){
total[,i]<-as.numeric(total[,i])
}
}
levels(tem\(Country)[levels(tem\)Country)==“United States”]<-“USA”
levels(tem\(Country)[levels(tem\)Country)==“United Kingdom”]<-“UK”
levels(tem\(Country)[levels(tem\)Country)==“Cura癟ao”]<-“Curacao”
levels(tem\(Country)[levels(tem\)Country)==“Saint Barth矇lemy”]<-“Saint Barthelemy”
levels(tem\(Country)[levels(tem\)Country)==“Saint Pierre and Miquelon”]<-“Saint Pierre And Miquelon”
tem<-tem[which(tem\(Country%in%levels(as.factor(tem\)Country))[which(levels(as.factor(tem\(Country))%in%levels(as.factor(world\)region)))]),]
tem\(Country<-as.factor(as.character(tem\)Country))
Name<-levels(tem\(Country) for(i in 1:length(Name)) for(k in min(as.numeric(as.character(tem[which(tem\)Country==Name[i]),]\(year))):2013){ df<-tem[tem\)year==k,]%>%
group_by(Country)%>%
summarise(tem=mean(tem))
df\(Country<-as.character(df\)Country)
df<-as.data.frame(df)
k<-as.character(k)
df1<-tem[which(as.numeric(as.character(tem\(year))>1950&as.numeric(as.character(tem\)year))<1980),]%>%
group_by(Country,year)%>%
summarise(avg=mean(tem))
df1<-df1%>%
group_by(Country)%>%
summarise(avg=mean(avg))
df1<-as.data.frame(df1)
total[which(total$region==Name[i]),k]<-(df[which(df$Country==Name[i]),“tem”])-(df1[which(df1$Country==Name[i]),“avg”])
}
ani.options(convert=“C:\Program Files\ImageMagick-7.0.5-Q16\magick.exe”)
saveGIF({
for(i in 7:ncol(total)){
plot1<-ggplot(total)+
geom_polygon(aes(x=long, y=lat,group=group,fill=total[,i]))+
labs(x=“”,y=“”,fill=“°C”,title=paste(“Year:”,names(total)[i]))+
scale_fill_gradient2(limit=c(-3,3),low =“blue”, high = “red”)+
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
legend.position = “bottom”,
legend.key.width = unit(2,“cm”),
legend.margin =margin(t = -1.3,b = -.1,unit = “cm”) ,
panel.background = element_blank(),
panel.grid = element_blank())
print(plot1)
}
},movie.name = “test13.gif”,interval=.15)