Sample data,
case_study1 <- structure(c(18.1265311110765, 18.1445806138217, 20.29444403667, 19.3943562610075, 18.4467029515654, 19.1106318701059, 18.913005082868, 20.1609526062384, 19.4320843210444, 19.3427841216326, 17.5637686550617, 21.8841708265245, 18.4573832275346, 16.7219386268407, 17.2237817412242, 18.5469768662006, 18.4829838024452, 20.7631137836725, 31.0647214353085, 29.5135084576905, 32.6102900002152, 29.3641985105351, 25.1434186547995, 27.994190084748, 27.0048756664619, 27.5333320870996, 31.3106615673751, 41.3828093148768, 40.9396879393607, 40.9130710875615, 37.9195794519037, 35.6223422279581, 39.8471474340186, 35.4306885786355, 38.9832761064172, 39.5527733126655, 50.0248330328614, 49.7703500119969, 51.4038161402568, 47.6908210013062, 44.733499577269, 49.0990697136149, 47.0381244253367, 50.6246182974428, 50.8727697608992, 48.1883749496192, 49.9094971902668, 51.4024006286636, 47.858812619932, 43.8532895939425, 51.0264792805538, 47.2120952093974, 50.0444536274299, 52.3428046917543, 59.7227923031896, 58.1244931928813, 60.2706275274977, 59.0147362891585, 52.6608879743144, 62.2188456263393, 57.7123434329405, 58.3746858304366, 62.6367503125221, 60.9077196735889, 56.8138210857287, 59.812376066111, 60.5437361020595, 51.2970413453877, 60.3391999211162, 57.3215152909979, 56.4301307220012, 62.8979858038947, 50.1909973882139, 45.9235293632373, 49.24725244008, 52.3605857389048, 42.8149003852159, 51.3142714761198, 47.4335271269083, 47.0058694463223, 52.0639638807625, 41.0728860516101, 36.0847078282386, 39.904410911724, 42.5762362144887, 32.67773448769, 42.9504046197981, 35.6693080635741, 37.4426508890465, 40.1027930378914), .Dim = 9:10, .Dimnames = list(Year = c("2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"), Month = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")))
case_study1
# Month
# Year 1 2 3 4 5 6 7 8 9 10
# 2007 18.12653 19.34278 31.06472 41.38281 50.02483 48.18837 59.72279 60.90772 50.19100 41.07289
# 2008 18.14458 17.56377 29.51351 40.93969 49.77035 49.90950 58.12449 56.81382 45.92353 36.08471
# 2009 20.29444 21.88417 32.61029 40.91307 51.40382 51.40240 60.27063 59.81238 49.24725 39.90441
# 2010 19.39436 18.45738 29.36420 37.91958 47.69082 47.85881 59.01474 60.54374 52.36059 42.57624
# 2011 18.44670 16.72194 25.14342 35.62234 44.73350 43.85329 52.66089 51.29704 42.81490 32.67773
# 2012 19.11063 17.22378 27.99419 39.84715 49.09907 51.02648 62.21885 60.33920 51.31427 42.95040
# 2013 18.91301 18.54698 27.00488 35.43069 47.03812 47.21210 57.71234 57.32152 47.43353 35.66931
# 2014 20.16095 18.48298 27.53333 38.98328 50.62462 50.04445 58.37469 56.43013 47.00587 37.44265
# 2015 19.43208 20.76311 31.31066 39.55277 50.87277 52.34280 62.63675 62.89799 52.06396 40.10279
Similar plot, using matplot
(to reduce your iteration):
cols <- c("red", "blue", "yellow", "chartreuse", "chocolate", "brown", "burlywood", "coral", "cyan")
matplot(
t(case_study1), type='b', pch=1, lty=1, col=cols,
xlim = c(1, ncol(case_study1) + 3),
xlab = "Month", ylab = "Temperature",
main = "Temperature chart")
legend("bottomright", legend = 2007:2015, col = cols, pch = 1)

To adapt this to your code using multiple calls to lines
, add xlim=
to your first plot
call, then add the legend(..)
call.
To do this in ggplot2
is not cosmic, but it should be melt
ed into a long format.
melted <- reshape2::melt(case_study1)
ggplot(melted, aes(Month, value, color = factor(Year), group = Year)) +
geom_line() +
labs(title = "Temperature chart", x = "Month", y = "Temperature") +
scale_color_discrete(name = "Year") +
scale_x_continuous(breaks = seq(1, 10, by = 2))
