#climate.r

climateData<-read.csv("climate.csv", header=TRUE)
#Read in the data file.
#To read in to R, the xlsx file first had to be merged in to a single sheet and saved as a .csv file

#test<-rowsum(climateData[2:13,4,])

#Create a new data frame; cumulativeData, that shows the cumulative rainfall and number of sunshine hours for each city

SwanRain<-rowSums(climateData[4,3:14])
SwanSun<-rowSums(climateData[7,3:14])
WuRain<-rowSums(climateData[11,3:14])
WuSun<-rowSums(climateData[14,3:14])
#To show the cumulative data of rainfall and hours of sun in the two cities, I have extracted the relevant rows and totaled to values contained.

cumulativeData<-data.frame(SwanRain,SwanSun,WuRain,WuSun)
cumulativeData
#the resulting cumulative data for each city are here combined in to a single data frame and displayed

#Include a function displayGraph() that will take the city name weather parameter and will produce a line graph of that weather over the year for that city. Display humidity in Wuhan and rainfall in Swansea.

#Define X as the data row we are looking to generate a line graph from; for this assignment question, row 12 is humidity in Wuhan, row 4, rainfall in Swansea
x<-12

#Creating the displayGraph() function, where "x" will be the input as dictated above.
displayGraph<-function(x)
{
  ydata<-1:12  #ydata will form the Y axis; the 12 months of the year
  xdata<-climateData[x,3:14]  #xdata will extract the data needed for the X axis, excluding the row labels.
  plot(as.numeric(ydata), xdata, type="l", xlab="Month", ylab=climateData[x,1], main=climateData[x,1:2])
  #The plot function in R will create a graph. The first inputs are the vectors, Y axis data followed by X axis data. Next, the graph type is specified, "l" for a line graph. The axis can be labeled. For this function, the label for the Y axis is pulled from the row in the data frame. Lastly, "main" sets the title, which I am again pulling from the data frame, with the location this time included. 
}
displayGraph(x)
#Finally, displayGraph(x) runs the function, on whatever "x" has been defined as to produce the line graph.


#Include a function compareCities() that will take the start month and end month and two weather parameters, and will plot the data for those parameters for both cities on the same graph. 

#Setting up inputs
#Start date
s<-1 #the numeric value for the desired month
#End date
e<-6
#Swansea Weather parameter 1
sw1<-climateData[1,3:14]
#Swansea weather parameter 2
sw2<-climateData[7,3:14]
#Wuhan weather parameter 1
wu1<-climateData[8,3:14]
#Wuhan weather parameter 2
wu2<-climateData[4,3:14]
ydata<-1:12  #ydata will form the Y axis; the 12 months of the year

compareCities<-function(s,e,sw1,sw2,wu1,wu2)
{
  if(sw1==5.2) #mean temperature should plot a line graph. Using the first value from the temperature column to identify if it is that column, and to react.
  {
    par(mar=c(5,5,3,5)) #neaten margin size
    barplot(as.numeric(sw2[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="") 
    #the barplot goes first to appear behind the line graph
    #as.numeric to ensure the data plots
    #including [s:e] to take the desired start and end date for the plot
    #xaxt and yaxt along with the blank labels stops axis plots overlaying messily
    par(new = TRUE) #to plot a second graph on the same table
    barplot(as.numeric(wu2[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="", col="orange") #now using colour to seperate plots
    par(new = TRUE) #to plot another graph on the same table
    plot(ydata[s:e], as.numeric(sw1[s:e]), type="l", xlab="Month", ylab="Average Temp.", col="blue", main="Comparing Climate Date between Swansea and Wuhan" ) #the graph title is set with "main"
    par(new = TRUE)
    plot(ydata[s:e], as.numeric(wu1[s:e]), type="l", xaxt="n", yaxt="n", xlab="", ylab="", col="yellow")
    axis(side=4)
    mtext("Average Sun (hours)", side=4)
    legend("bottom", c("Swansea Temp","Swansea Sun", "Wuhan Temp","Wuhan Sun"), col=c("Blue","Grey","Yellow","Orange"), lty=c(1,2))
  }
  else if(sw1==0.86) #humidity should plot a line graph. Using the first value from the humidity column to identify if it is that column, and to react.
  {
    par(mar=c(5,5,3,5)) 
    barplot(as.numeric(sw2[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="") 
    par(new = TRUE) 
    barplot(as.numeric(wu2[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="", col="orange") 
    par(new = TRUE) 
    plot(ydata[s:e], as.numeric(sw1[s:e]), type="l", xlab="Month", ylab="Average Temp.", col="blue", main="Comparing Climate Date between Swansea and Wuhan" ) 
    par(new = TRUE)
    plot(ydata[s:e], as.numeric(wu1[s:e]), type="l", xaxt="n", yaxt="n", xlab="", ylab="", col="yellow")
    axis(side=4)
    mtext("Average Sun (hours)", side=4)
    legend("bottom", c("Swansea Humidity","Swansea Sun", "Wuhan Humidity","Wuhan Sun"), col=c("Blue","Grey","Yellow","Orange"), lty=c(1,2))
  }
  else if(sw2==5.2) #mean temperature should plot a line graph
  {
    par(mar=c(5,5,3,5)) 
    barplot(as.numeric(sw1[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="") 
    par(new = TRUE) 
    barplot(as.numeric(wu1[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="", col="orange") 
    par(new = TRUE) 
    plot(ydata[s:e], as.numeric(sw2[s:e]), type="l", xlab="Month", ylab="Average Temp.", col="blue", main="Comparing Climate Date between Swansea and Wuhan" ) 
    par(new = TRUE)
    plot(ydata[s:e], as.numeric(wu2[s:e]), type="l", xaxt="n", yaxt="n", xlab="", ylab="", col="yellow")
    axis(side=4)
    mtext("Average Sun (hours)", side=4)
    legend("bottom", c("Swansea Temp","Swansea Sun", "Wuhan Temp","Wuhan Sun"), col=c("Blue","Grey","Yellow","Orange"), lty=c(1,2))
  }
  else if(sw2==0.86) #humidity should plot a line graph
  {
    par(mar=c(5,5,3,5)) 
    barplot(as.numeric(sw1[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="") 
    par(new = TRUE) 
    barplot(as.numeric(wu1[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="", col="orange") 
    par(new = TRUE) 
    plot(ydata[s:e], as.numeric(sw2[s:e]), type="l", xlab="Month", ylab="Average Temp.", col="blue", main="Comparing Climate Date between Swansea and Wuhan" ) 
    par(new = TRUE)
    plot(ydata[s:e], as.numeric(wu2[s:e]), type="l", xaxt="n", yaxt="n", xlab="", ylab="", col="yellow")
    axis(side=4)
    mtext("Average Sun (hours)", side=4)
    legend("bottom", c("Swansea Humidity","Swansea Sun", "Wuhan Humidity","Wuhan Sun"), col=c("Blue","Grey","Yellow","Orange"), lty=c(1,2))
  }
  
  else #anything else should plot a bar chart
  {
    par(mar=c(5,5,3,5)) 
    barplot(as.numeric(sw1[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="") 
    par(new = TRUE) 
    barplot(as.numeric(wu1[s:e],ydata[s:e]), xaxt="n", yaxt="n",xlab="", col="orange") 
    par(new = TRUE) 
    barplot(ydata[s:e], as.numeric(sw2[s:e]), xlab="Month", ylab="Average Temp.", col="blue", main="Comparing Climate Date between Swansea and Wuhan" ) 
    par(new = TRUE)
    barplot(ydata[s:e], as.numeric(wu2[s:e]), xaxt="n", yaxt="n", xlab="", ylab="", col="yellow")
    axis(side=4)
    mtext("Average Sun (hours)", side=4)
    legend("bottom", c("Swansea Humidity","Swansea Sun", "Wuhan Humidity","Wuhan Sun"), col=c("Blue","Grey","Yellow","Orange"), lty=c(1,2))
  }
}
compareCities(s,e,sw1,sw2,wu1,wu2)
