library("tidyverse")

#import data
monthly_temps <- read_csv("monthly_global_land_and_ocean_temperature_anomalies.csv")
## Parsed with column specification:
## cols(
##   YearMonth = col_double(),
##   Value = col_double()
## )
#changes it so it only records the number of each row and uses it as the x coordinate 
monthly_temps <- mutate(monthly_temps, month_number = row_number())

#allows for both year and month data to be dealt with together on the x
library("lubridate")
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
#converts to a date  class
class(monthly_temps$YearMonth)
## [1] "numeric"
monthly_temps <- mutate(monthly_temps, date = ymd(YearMonth, truncated=1))
class(monthly_temps$date)
## [1] "Date"
#month name not number 
monthly_temps <- mutate(monthly_temps,
                        year=year(date),
                        month=month(date,label=TRUE))
#rename variables 
monthly_temps <- rename(monthly_temps, temperature_anomaly = Value)
monthly_temps <- select(monthly_temps, -YearMonth)

#variables 
monthly_temps$month <- factor(monthly_temps$month)
monthly_temps$year <- factor(monthly_temps$year)
monthly_temps$temperature_anomaly <- as.numeric(monthly_temps$temperature_anomaly)

#line graph
ggplot()+ geom_line(data=monthly_temps, aes(x=month, y= temperature_anomaly, group=year, color=temperature_anomaly))+
  scale_color_gradientn(colors=rev(rainbow(2)))+theme_minimal()+
  labs(title="Historical Temperature Anomalies since 1880 by month",y= "Temperature anomalies in degrees celsius")

#spiralized graph
ggplot()+ geom_line(data=monthly_temps, aes(x=month, y= temperature_anomaly, group=year, color=temperature_anomaly))+
  scale_color_gradientn(colors=rev(rainbow(2)))+theme_minimal()+ coord_polar()+scale_x_discrete(expand=c(0,0))+
  labs(title="Historical Temperature Anomalies since 1880 by month",y= "Temperature anomalies in degrees celsius")

save(monthly_temps, file="climate_data.RData")