(Splitting plotting into several chunks causes problems for RMarkdown)
# First plot with Consumption data, setting the axis scale (ylim) automatically
plot(dateTime, Consumption, pch = 16, axes = FALSE, ylim = c(min(Consumption), max(Consumption)), xlab="", ylab="", type = "b", col = "dark orange", main = "Hourly energy consumption vs. temperature", sub = theDate)
axis(2, ylim = c(min(Consumption), max(Consumption)), col = "dark orange", col.axis = "dark orange", lwd = 2, las = 1)
mtext("Consumption per hour (kWh)", side = 2, col = "dark orange", line = 3)
box()
# Setting par to new so that we plot on top of the previous
par(new = TRUE)
# 2nd plot with Temperature data
plot(dateTime, Temperature, pch = 15, axes = FALSE, ylim = c(min(Temperature), max(Temperature)), xlab="", ylab="", type ="b", col = "dark violet")
axis(4, ylim = c(min(Temperature), max(Temperature)), col = "violet", col.axis = "dark violet", lwd = 2)
mtext("Temperature (°C)", side = 4, col = "dark violet", line = 3) # Rmd still not showing 2nd y axis title..?
# Creating the x axis here with sequence of hours 1-24
r <- as.POSIXlt(Start)
axis.POSIXct(1, at = seq(r[1], r[24], by = "hour"), format = "%H")
mtext("Time (Hours)", side = 1, col = "black", line = 3)
# Comment to make code more readable
legend("topleft", legend = c("Consumption", "Temperature"), text.col = c("dark orange", "dark violet"), pch = c(16,15), col = c("dark orange", "dark violet"))
