0

I am trying to apply the answer to my prior question on plotting with dates in the x axis to the COVID data in the New York Times but I get an error message:

require(RCurl)
require(foreign)
require(tidyverse) 

counties = read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv", sep =",",header = T)

Philadelphia <- counties[counties$county=="Philadelphia",]
Philadelphia <- droplevels(Philadelphia)
rownames(Philadelphia) <- NULL

with(as.data.frame(Philadelphia),plot(date,cases,xaxt="n"))

axis.POSIXct(1,at=Philadelphia$date,
             labels=format(Philadelphia$date,"%y-%m-%d"),
             las=2, cex.axis=0.8)
# Error in format.default(structure(as.character(x), names = names(x), dim = dim(x),  : 
# invalid 'trim' argument

The structure of the data includes already a date format:

> str(Philadelphia)
'data.frame':   21 obs. of  6 variables:
 $ date  : Factor w/ 21 levels "2020-03-10","2020-03-11",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ county: Factor w/ 1 level "Philadelphia": 1 1 1 1 1 1 1 1 1 1 ...
 $ state : Factor w/ 1 level "Pennsylvania": 1 1 1 1 1 1 1 1 1 1 ...
 $ fips  : int  42101 42101 42101 42101 42101 42101 42101 42101 42101 42101 ...
 $ cases : int  1 1 1 3 4 8 8 10 17 33 ...
 $ deaths: int  0 0 0 0 0 0 0 0 0 0 ...

I tried changing the axis call to

axis.Date(1,Philadelphia$date, at=Philadelphia$date,
          labels=format(Philadelphia$date,"%y-%m-%d"),
          las=2, cex.axis=0.8)

without success.

I wonder if it has to do with the strange horizontal lines in the plot (as opposed to points):

enter image description here

Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114
  • 1
    Make sure your date column is formatted as a date! Right now according to your output it's a factor. Something like this could help: https://stackoverflow.com/questions/17496358/r-help-converting-factor-to-date – MrFlick Apr 01 '20 at 03:40

1 Answers1

2

The 'invalid trim argument' error comes from format (it is the default second argument because you haven't explicitly specified the parameter).

I'm not entirely sure what you're doing here but I would change date to a Date object before plotting the data. You'll also want to use %Y instead of %y I believe.

library(dplyr)

counties = read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv", sep =",",header = T)

Philadelphia <- counties[counties$county=="Philadelphia",] %>%
    mutate(date = as.POSIXct(date, format = '%Y-%m-%d'))

with(Philadelphia, plot(date,cases))

img

heds1
  • 3,203
  • 2
  • 17
  • 32