0

I currently have a time series graph of COVID-19 cases in the United States; however, instead of listing the dates (e.g., 04/01/2020, 04/02/2020, ...), it uses the index of the date along the x-axis.

Here is what my data looks like

    date new.cases
1 4.1.20     32243
2 4.2.20     32235
3 4.3.20     32287
4 4.4.20     32416
5 4.5.20     29877
6 4.6.20     31381
...

How can I make these dates show up along the x-axis?

Note: I am using standard R to plot this, no special packages.

plot(dat$new.cases, type = "l", xlab = "Date", ylab = "Cases",
     main = "COVID-19 Cases in the United States")

enter image description here

325
  • 594
  • 8
  • 21
  • 1
    See here - https://stackoverflow.com/questions/24481176/r-x-axis-date-labels-using-plot - you need to include the `dat$date` column in your call, after converting it to a Date object. If you need fine-toothed control, you suppress the axis first, then add `axis.Date` with the exact format/tick mark locations you want. The accepted answer at the linked question is pretty much exactly what I would have answered here. – thelatemail Feb 15 '21 at 21:38
  • 1
    You can convert to be as follows `plot(as.Date(dat$date,"%m.%d.%y"),dat$new.cases)` – Evan Friedland Feb 15 '21 at 23:36

1 Answers1

0

Use this code to accomplish this:

plot(as.Date(dat$date,"%m.%d.%y"), dat$new.cases, type = "l", xlab = "Date", ylab = "Cases",
     main = "COVID-19 Cases in the United States")
325
  • 594
  • 8
  • 21