# Zihui Xiao, 2106354, MSc Data and Artificial Intelligence

# task1-1
# Load the data
# Download the readxl installation package and import it
library(readxl)

# mport the data of two sheets in Excel
# the city is imported as a factor
climateData_Swansea<-read_xlsx("2021Rstudio-study/task2/climate.xlsx",sheet="Swansea Processed")
climateData_Swansea<-cbind(climateData_Swansea,city="Swansea")

climateData_Wuhan<-read_xlsx("2021Rstudio-study/task2/climate.xlsx",sheet="Wuhan Processed")
climateData_Wuhan<-cbind(climateData_Wuhan,city="Wuhan")

# Merge datasets
climateData<-rbind(climateData_Swansea,climateData_Wuhan)
# display the  data frame
climateData

# task1-2
# Create a new data frame cumulativeData 
# Show the cumulative rainfall and cumulative number of hours of sunshine for each city

# Use the for loop to calculate the cumulative rainfall of Swansea
precipitation_Swansea<-0

for (i in 2:13){
  precipitation_Swansea<-precipitation_Swansea+climateData[4,i]
}

# Use the for loop to calculate the cumulative rainfall of Wuhan
precipitation_Wuhan<-0

for (i in 2:13){
  precipitation_Wuhan<-precipitation_Wuhan+climateData[11,i]
}

# Combine the data
precipitation<-c(precipitation_Swansea,precipitation_Wuhan)

# Calculate the cumulative sunshine hours of Swansea
Sunhours_Swansea<-climateData[7,2]*31+climateData[7,3]*28+climateData[7,4]*31+
                  climateData[7,5]*30+climateData[7,6]*31+climateData[7,7]*30+
                  climateData[7,8]*31+climateData[7,9]*31+climateData[7,10]*30+
                  climateData[7,11]*31+climateData[7,12]*30+climateData[7,13]*31
Sunhours_Swansea

# Calculate the cumulative sunshine hours of Wuhan
Sunhours_Wuhan<-climateData[14,2]*31+climateData[14,3]*28+climateData[14,4]*31+
                climateData[14,5]*30+climateData[14,6]*31+climateData[14,7]*30+
                climateData[14,8]*31+climateData[14,9]*31+climateData[14,10]*30+
                climateData[14,11]*31+climateData[14,12]*30+climateData[14,13]*31
Sunhours_Wuhan

# Merge the data
Sunhours<-c(Sunhours_Swansea,Sunhours_Wuhan)

city<-c("Swansea","Wuhan")

# Create a new data frame cumulativeData and display it
cumulativeData<-data.frame(city,precipitation,Sunhours,stringsAsFactors=FALSE)
cumulativeData


# task1-3
# Include a function displayGraph()
# This function displays the weather parameters of different cities in a line chart
# Display this using humidity in Wuhan and rainfall in Swansea

# Install the package
install.packages(plotrix)
library(plotrix)

displayGraph<-function(df)
{
  x<-c(1:12)
  # the data of ainfall in Swansea
  y_Swansea<-as.numeric(df[4,2:13])
  # the data of humidity in Wuhan
  y_Wuhan<-as.numeric(df[12,2:13])
  
  # Draw a line chart to display the weather parameters of different cities
  twoord.plot(lx=x, ly=y_Swansea,
              rx=x,ry=y_Wuhan,  # the values of the left and right axes
              
              # limit the range of abscissa values and left and right ordinate values
              xlim=c(1,12),  
              lylim=c(min(y_Swansea),max(y_Swansea)), 
              rylim=c(min(y_Wuhan),max(y_Wuhan)),
              lytickpos = c(76,150),  # sets the position of the left axis scale label
              lcol='red',rcol='blue',  # the color of the left and right coordinate axes
              
              # Set title and label
              main="the line graph of that weather parameter over the year",
              xlab="Month", 
              ylab="Precipitation / Rainfall mm", rylab="Humidity(%)", 
              type=c("o","o"))
              # Add legend
              legend("top",c("Swansea","Wuhan"),pch=c(1,2),col=c("red","blue"),bg ="white")
  
}

displayGraph(climateData)



# task1-4
# Include a function compareCities()
# take the start month and end month and two weather parameters, and plot the data for those parameters for both cities on the same graph
compareCities<-function(df)
{
  # Get the climate parameters of the two cities in different months
  x<-c(1:12)
  y_Swansea_AvgTemp<-as.numeric(df[1,2:13])
  y_Swansea_AvgSun<-as.numeric(df[7,2:13])
  y_Wuhan_AvgTemp<-as.numeric(df[8,2:13])
  y_Wuhan_AvgSun<-as.numeric(df[14,2:13])
  
  
  # Draw a bar chart of the average sunshine duration of the two cities in different months
  data1<-NULL
  for (i in 1:12){
    data1<-c(data1,y_Swansea_AvgSun[i],y_Wuhan_AvgSun[i])
  }
  
  data1<-matrix(data1,nrow=2)
  # Name rows and columns
  colnames(data1) <- c(1:12)
  rownames(data1) <- c("Swansea","Wuhan")
  
  # Draw the bar chart
  barplot(data1,
          main = "the camparison of weather parameters over the year",
          xlab = "Month",
          ylab = "Avg. Sun hours (hours)/Avg. Temperature °C",
          ylim = c(0,30),
          col=c("green","blue"),
          legend=c("Sunhours in Swansea","Sunhours in Wuhan"),
          beside = TRUE)
  
  
  #Add the lines of the average temperature of two cities in different months
  lines(x=x*3,y_Swansea_AvgTemp,main="the camparison of weather parameters over the year",
        col="red",type = "o")
  lines(x=x*3,y_Wuhan_AvgTemp,main="the camparison of weather parameters over the year",
        col="black",type = "o")
  legend("topleft",c("Temperature in Swansea","Temperature in Wuhan"),
         pch=c(1,2),col=c("red","black"))
  
}

compareCities(climateData)
