0

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:

enter image description here

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?

  • `format` returns a string, not a date object. (see output of `str(format(as.Date(as.POSIXct("2020-03-24", "%d-%m-%Y"))), "%m-%Y")`) – dario Feb 09 '21 at 14:49

2 Answers2

1

Here are two solutions base R and ggplot2.

1. Base R

To plot multiple lines use either matplot or matlines.

colrs <- c("darkgreen", "red", "blue")
matplot(df[[1]], df[-1], 
        type = "l", lty = "solid", lwd = 1,
        col = colrs,
        xlab = "date", ylab = "$", xaxs = "i")
legend("bottomleft", inset = 0.04, legend = c("a", "b", "c"),
       col = colrs, lwd = 3, cex = 0.8)

enter image description here

2. ggplot2

The data is in the wide format and one line per column vector is to be plotted against an x axis vector, in this case the date vector. This sort of problem is usually a data reformating problem. See reshaping data.frame from wide to long format.

library(ggplot2)
library(dplyr)
library(tidyr)

df %>%
  pivot_longer(-date) %>%
  ggplot(aes(date, value, colour = name)) +
  geom_line() +
  scale_colour_manual(breaks = c("a", "b", "c"), values = c("darkgreen", "red", "blue")) +
  scale_x_date(date_labels = "%Y-%m") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 60, vjust = 0.5, hjust = 0.5))

enter image description here

Data

df <- read.table(text = "
        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
", header = TRUE)
df$date <- as.Date(df$date)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0

You replace the values of the x axis with the years in your dataset by adding xaxt="n" to your plot() command (which removes the current x axis) and calling

axis(1,at=1:nrow(df),labels=format(as.Date(df$date),"%Y"))

afterwards (which creates the desired x axis).