cocoa <- read.csv ("Cocoa_prices.csv", header = FALSE)
Mths <- substr(cocoa$V1, start = 1, stop = 3) 
Yrs <- paste ("20", substr ( cocoa$V1, start = 5, stop = 6)) 
cocoa_updated <- data.frame (Yrs, Mths, cocoa$V2)
Price <- cocoa_updated$cocoa.V2
cocoa_updated_price <- data.frame (Yrs, Mths, Price)
cocoa_wide <- reshape ( cocoa_updated_price, timevar = "Yrs", idvar = "Mths", direction = "wide")
colnames (cocoa_wide) <- c ( colnames (cocoa_wide)[1], "2012", "2013", "2014")


##### plot the first line of year 2012
plot(cocoa_wide$"2012",
     xlab = "months", ylab = "price ($)", ylim = c (2100, 3300), 
     main = "Cocoa Prices from 2012 to 2014",
     col = "black",
     xaxt='n', bty = "l")

months <- as.character( cocoa_wide$Mths)
axis (side = 1, at = 1:12, labels = months)

lines(cocoa_wide$"2012", col = "black", lwd= 3)

abline ( h = mean (cocoa_wide$"2012"), 
         lty = 3, lwd = 2, 
         col = "black")

# plot the second line of year 2013
points(cocoa_wide$"2013", col = "deepskyblue", pch = 15)
lines(cocoa_wide$"2013", col = "deepskyblue", lty = 1, lwd = 3)
abline ( h = mean (cocoa_wide$"2013"), 
         lty = 3, lwd = 2, 
         col = "blue")

# plot the last line of the year 2014
points(cocoa_wide$"2014", col = "red", pch = 20)
lines(cocoa_wide$"2014", col = "red", lty = 1, lwd = 3)
abline ( h = mean (cocoa_wide$"2014"), 
         lty = 3, lwd = 2, 
         col = "red")

# add the legend and text
legend ( x = 10, y = 2300, legend = c ("2012", "2013", "2014"),
         col = c ( "black", "blue", "red"),
         lty = rep ( 1 , times = 3), cex = 0.8)
text (x = 3.5, y = 2700,
      labels = paste ("The dotted lines are annual means", 
                      sep = ""), 
      font = 3, col = "black")

# add means
text (x = 10, y = mean(cocoa_wide$"2012") -50, 
      labels = substitute (paste ("mean:",  
                                  # Substitute() function could be used to show greek letters, 
                                  # but leaves alone any text in quotation marks.
                                  mu[ 2012],   # The [] here is completely different from the one we have used so far,
                                  # They tell the substitute() function to display whatever is inside them
                                  # as a subscript  (^ for superscripts)
                                  " = $", 
                                  a),
                           list (a = round (mean (cocoa_wide$"2012"), 
                                            0), 
                                 sep = "")
      ), 
      font = 3, col = "black", cex = 0.8)

text (x = 4, y = mean(cocoa_wide$"2013") +50, 
      labels = substitute (paste ("mean:", mu[ 2013], " = $", a),
                           list (a = round (mean (cocoa_wide$"2013"), 
                                            0), 
                                 sep = "")
      ), 
      font = 3, col = "blue", cex = 0.8)

text (x = 8, y = mean(cocoa_wide$"2014") +50, 
      labels = substitute (paste ("mean:", mu[ 2014], " = $", a),
                           list (a = round (mean (cocoa_wide$"2014"), 
                                            0), 
                                 sep = "")
      ), 
      font = 3, col = "red", cex = 0.8)