I loaded in a dataset to R that looks like this in the header:
date a b c
1 2017-01-01 -0.98 -1.35 -2.81
2 2017-02-01 -1.63 -2.18 -1.79
3 2017-03-01 -0.92 0.80 -3.33
4 2017-04-01 0.44 0.48 -2.11
5 2017-05-01 1.46 -3.11 -3.67
6 2017-06-01 -0.32 2.46 1.45
The full dataset includes 4 years of data with a total of 48 obs (from Jan-2017 to Dec-2020).
After loading in the dataset I change the format of the date variable to YYYYMM by using the code:
df$date <- format(as.Date(df$date), "%Y%m")
This results in the dates looking like this:
date a b c
1 201701 -0.98 -1.35 -2.81
2 201702 -1.63 -2.18 -1.79
3 201703 -0.92 0.80 -3.33
4 201704 0.44 0.48 -2.11
5 201705 1.46 -3.11 -3.67
6 201706 -0.32 2.46 1.45
After doing this I plot the data with this code:
plot(df$a, type="l", col="darkgreen", lwd=1, xlab="date", ylab="$", xaxs="i")
lines(df$b, col="red", lwd=1, xaxs="i")
lines(df$c, col="blue", lwd=1, xaxs="i")
legend("bottomleft", inset= 0.04, legend=c("a", "b", "c"),
col=c("darkgreen", "red", "blue"), lwd=3, cex=0.8)
Which results in the plot below:
However, the values of the x-axis do not show me the years so that I can measure the performance of a, b and c over time. How do I replace the values of the x-axis with the years in my dataset. And also, how do I make sure that only the years will be included on my x-axis and not my months as well?
The answers to this question I've seen so far has been to format the date etc. This is done and seems to work fine. Can anyone please tell me what to do about this issue?