                                      ### Brandon Gorman | 1909188 ###
                                                ### TASK ONE  ###


#Question One

climateData <- read.csv("climate.csv", header = TRUE, stringsAsFactors = TRUE)    #Insert standardised data set, with added row to identify city.
                                                                                 #Convert the city information to a factor. 
climateData           #Preview custom data set.



#Question Two

cumulativeData <- climateData [c(7,4,14,11),]   #Create a new data frame by extracting the rows containing both cities Avg. Sun hours (hours) and Precipitation / Rainfall mm.


cumulativeDataSunWuhan <- as.numeric(cumulativeData[c(1),(3:14)])       #Assign each row of data to a vector, by converting to a number.
cumulativeDataRainfallWuhan <- as.numeric(cumulativeData[c(2),(3:14)])
cumulativeDataSunSwansea <- as.numeric(cumulativeData[c(3),(3:14)])
cumulativeDataRainfallSwansea <- as.numeric(cumulativeData[c(4),(3:14)])

cumulativeDataSunWuhan <- cumsum(cumulativeDataSunWuhan)                   #Although a for loop could be used, they are slow in comparison to a function (cumsum), if a data set were to grow the function would work faster.
cumulativeDataRainfallWuhan <- cumsum(cumulativeDataRainfallWuhan)
cumulativeDataSunSwansea <- cumsum(cumulativeDataSunSwansea)
cumulativeDataRainfallSwansea <- cumsum(cumulativeDataRainfallSwansea)

cumulativeCity <- c("Wuhan","Wuhan","Swansea","Swansea")                # The vector will assist in the identification of each row.


cumulativeData<-rbind(cumulativeDataSunWuhan,cumulativeDataRainfallWuhan,
                      cumulativeDataSunSwansea,cumulativeDataRainfallSwansea)   #The rbind will take the summed vectors and turn them into a data frame.


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

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

cumulativeData  #View cumulative rainfall



#Question Three 

displayGraph <- function(){                                                #Create a custom function titled displayGraph() which will produce a line graph visual.
  climateMonth <- c("January", "February","March", "April" ,"May",         #Create a month vector to measure the time, in this case month.
                    "June" ,"July" ,"August","September" ,"October"
                    ,"November", "December")
  
  climateHumidity <- 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 that contains Wuhan's humidity.
  climateRainfall <- 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 a vector that contains Swansea's rainfall.
  
  climateMonth_Name <- "Month"                       #Create a vector for each column name.
  climateHumidity_Name <- "Wuhan_Humidity"
  climateRainfall_Name <- "Swansea_Rainfall"
  
  climateGraph <- data.frame(climateMonth,climateHumidity,climateRainfall)        #Combine the data together and convert the vectors to a data frame.
  names(climateGraph) <- c(climateMonth_Name, climateHumidity_Name, climateRainfall_Name)   #Assign the column names to the data frame to assists in data identification.

  
  par(mar = c(5, 5, 3, 5))     #Provides a neater margin size.
  plot(climateGraph[1:12, 2], type ="l", ylab = "Humidity",
       main = "Wuhan Humidity against Swansea Rainfall.", xlab = "Month",
       col = "blue")
  
  par(new = TRUE)                                              #Enables an additional data set be associated with the same line graph.
  plot(climateGraph[,3], type = "l", xaxt = "n", yaxt = "n",    #xaxt & yaxt with nothing associated ensures any axis overlay.
       ylab = "", xlab = "", col = "darkgreen", lty = 2)
  axis(side = 4)
  mtext("Swansea Rainfall", side = 4, line = 3)
  legend("bottom", c("Humidity", "Rainfall"),
         col = c("blue", "darkgreen"), lty = c(1, 2))
}

displayGraph()   #Run function to output graph.



#Question Four

compareCities <- function(){                        ##Create a custom function titled compareCities() which will produce a graph visual.
  startNumber <- 1                                  #A start measure, to measure the start month. January.
  endNumber <- 12                                   #An end measure, to measure the end month. December.
  yAx <- 1:12                                       #yAx will provide the visual for the months (y axis.)
  
  swanseaTemperature <- climateData[1,3:14]             #Using a initial data set, extract the relevant data.
  swanseaSun <- climateData[7,3:14]
  
  wuhanTemperature <- climateData[3,3:14]
  wuhanSun <- climateData[4,3:14]
  
  
  par(mar = c(5, 5, 3, 5))                          #Provides a neater margin size.
  barplot(as.numeric(swanseaSun[startNumber:endNumber], yAx[startNumber:endNumber]), xaxt="n", 
          yaxt="n", xlab="")                        #The barplot is to display the data. The startNumber and endNumber is a measure, to measure the time period.
                                                    #xaxt, yaxt & xlab ensures no label overlay etc.
  par(new = TRUE)                                   #Allows for a new set of data to be placed on the same chart.
  barplot(as.numeric(wuhanSun[startNumber:endNumber],yAx[startNumber:endNumber]),xaxt="n", 
          yaxt="n", xlab="", col="darkblue")
  
  par(new = TRUE)
  plot(yAx[startNumber:endNumber], as.numeric(swanseaTemperature[startNumber:endNumber]), type = "l",   
       xlab = "Month (Gregorian Calendar)", ylab = "Avg. Temperature °C", col="yellow",      #As the data has been collected and interpreted in the Gregorian Calendar, highlighting that the year is in the Gregorian Calendar will remove any potential confusion to international individuals.
       main = "A comparison of climate data between Swansea and Wuhan.")     #Main command allows for a meaningful title to describe what the chart is showing.
  
  par(new = TRUE)
  plot(yAx[startNumber:endNumber], as.numeric(wuhanTemperature[startNumber:endNumber]), type = "l",
       xaxt="n", yaxt="n", xlab="" , ylab = "", col='red' )
  
  axis(side = 4)
  
  mtext('Avg. Sun hours (hours)', side = 4)     #Provides a title to the right-hand side of the chart.
  
  legend("bottom", c("Avg.Swansea Temperature °C" , "Avg.Swansea Sun Hours","Avg.Wuhan Temperature °C" , "Avg.Wuhan Sun Hours"),
         col = c("yellow", "gray", "red", "darkblue"), lty = c(1,2))
  
}
compareCities() #Run function to output graph.