-------------------------------
Title: "Assignment 2 Task 1"
Student: "Emily Stockdale"
Studentnumber: "1906962"
-------------------------------

#Q1

#load data file into data frame climatedata 
#data has been loaded from 2 excel spreadsheets to one data file and saved as csv

#read in data using read.csv while importing headings and ensuring City is imported as factor, string as factors used to apply this.
  
climatedata<-read.csv("climate.csv", header=TRUE, stringsAsFactors =TRUE)

#display climatedata

climatedata


#Q2

#create a new data frame by extracting the data climate data
cumulativedata<-climatedata[c(7,4,14,11),] 

#display cumulative data

cumulativedata

#assign each row of data to a vector by converting to a number to enable binding into a data frame later

sunwuhan<-as.numeric(cumulativedata[c(1),(3:14)]) 
rainwuhan<-as.numeric(cumulativedata[c(2),(3:14)])
sunswansea<-as.numeric(cumulativedata[c(3),(3:14)])
rainswansea<-as.numeric(cumulativedata[c(4),(3:14)])

#cumsum used to accumulate the data, a loop could be used they are slower with larger data frames

sunwuhan<-cumsum(sunwuhan)  
rainwuhan<-cumsum(rainwuhan)
sunswansea<-cumsum(sunswansea)
rainswansea<-cumsum(rainswansea)

#row identification

cityID<-c("wuhan","wuhwan","swansea","swansea") 

#rbind will comine the data and turn it into a data frame

cumulativedata<-rbind(sunwuhan,rainwuhan,sunswansea,rainswansea)

#define column names for the new data frame

colnames(cumulativedata)<- c("January", "February","March", "April" ,"May", "June" ,"July" ,"August","September" ,"October","November", "December")

#define row names for new data frame

rownames(cumulativedata)<- c("Avg. Sun hours (hours) Wuhan", "Precipitation / Rainfall mm Wuhan","Avg. Sun hours (hours) Swansea", "Precipitation / Rainfall mm Swansea")

#Display data frame 

cumulativedata

#Q3 

#create function display graph

displaygraph<-function(){

#Create a month vector this will measure the time frame
  
climatemonth<-c("January","February","March","April","May","June","July","August","September","October","November","December") 

#Create a vector that contains Wuhan humidity.

wuhanhumidity <- c( 0.7, 0.73, 0.71, 0.72, 0.73, 0.78, 0.77, 0.76, 0.72, 0.7, 0.72,0.68 ) 

#create a vector containing Swansea rainfall

swansearainfall <-c (139.00, 99.00, 89.00, 76.00, 80.00, 86.00, 92.00, 106.00, 105.00, 49.00, 150.00, 150.00) 

#create vectors for the column names, this will be used later in a graph

month<-"month"                
humidity<-"humidity wuhan"
rainfall<-"rainfall swansea"

#create variable, this will be used for a graph this assigns the climate month, humidity and rainfall data.

graph<-data.frame(climatemonth,wuhanhumidity,swansearainfall)
names(graph)<-c(month,humidity,rainfall)

#provides a neater margin size

par(mar=c(5,5,3,5)) 

#plotting/assigning data to graph

plot(graph[1:12,2], type="l", ylab="humidity", 
     main="Comparison of Swansea Rainfall and Wuhan Humidity", xlab="month",
     col="pink")

#using par allows new data to be added to the graph in the future

par(new=TRUE)
plot(graph[,3], type="l", xaxt="n", yaxt="n",
     ylab="", xlab="", col="purple", lty=2)

axis(side=4)
mtext("rainfall swansea", side=4, line=3)
legend("bottom", c("Humidity", "Rainfall"),
      col = c("pink", "purple"), lty = c(1, 2))
         }  

#display graph

displaygraph()

#Q4

#define function 

comparecities<-function(){

#starting point   
  
month1<-1

#end point

month12<-12

#display plot

yax<-1:12 

#weather parameters for graph  

stemp<-climatedata[1,3:14]
ssun<-climatedata[7,3:14]
wtemp<-climatedata[3,3:14]
wsun<-climatedata[4,3:14]

#plotting/assigning data to graph and assigning each element a color while ensuring
#the correct data frames are displayed correctly, one as a barplot one as a line graph.

par(mar=c(5, 5, 3, 5))
barplot(as.numeric(ssun[month1:month12], yax[month1:month12]), xaxt="n",
        yaxt="n", xlab="")
        
par(new=TRUE)
barplot(as.numeric(wsun[month1:month12], yax[month1:month12]), xaxt="n",
        yaxt="n", xlab="", col="pink")

par(new=TRUE)
plot(yax[month1:month12], as.numeric(stemp[month1:month12]), type="l",
      xlab="month", ylab="Avg. Temperature °C", col="purple",
      main="Swansea and Wuhan Climate Comparison")

par(new=TRUE)
plot(yax[month1:month12], as.numeric(wtemp[month1:month12]), type="l",
     xaxt="n", yaxt="n", ylab="", xlab="", col="orange")

axis(side=4)

mtext('Avg. Sun hours (hours)', side=4)

#create graph legend, including where the legend should sit on the graph.

legend("bottom", c("Avg.Temperature °C Swansea" , "Avg.Sun Hours Swansea","Avg.Temperature°C Wuhan" , "Avg.Sun Hours Wuhan"),
      col = c("pink", "gray", "purple", "orange"), lty = c(1,2))

}

#display graph

comparecities()
