###### The plot for exercise 6.4. Cocoa Prices from 2012 to 2014
# add the price and mean lines for all three years using
# the a and b arguments for abline(), legend, text and axis features.
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.5)
text (x = 3, y = 3180,
labels = "The dotted lines are annual means",
font = 3, col = "gold2")
