0
votes

This is the code (which used to work):

require('RCurl')
require(repr) 
require(RCurl)
require(foreign)
require(tidyverse) 

states = read.csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv", sep =",",header = TRUE)
states <- states %>%
  mutate(date = as.POSIXct(date, format = '%Y-%m-%d'))

last_day <- states[states$date==states$date[nrow(states)],]

with(last_day, plot(state, cases, las=2, cex.axis=.9, cex.main=1.5,
                    main="Confirmed cases by state",
                    xlab="", ylab=""))

... and this is the error message:

Error in plot.window(...) : need finite 'xlim' values In addition: Warning messages: 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 2: In min(x) : no non-missing arguments to min; returning Inf 3: In max(x) : no non-missing arguments to max; returning -Inf

But there is no call for logarithmic plotting; and there are no infinite values:

> summary(last_day$cases)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
     70   23562   86525  134731  154429  831494 
2
Do you need a barplot? i.e. barplot(with(last_day, tapply(cases, state, FUN = sum)))akrun

2 Answers

2
votes

The only reason I can think of that something would have worked previously and then stopped working is if you changed from an implicit stringsAsFactors=TRUE to stringsAsFactors=FALSE (e.g. updating from R 3.x to 4.x, or changing a global option setting).

This works (ugly plot, but it works):

with(last_day,plot(factor(state),cases))

This would be my suggestion for a ggplot (almost) one-liner:

ggplot(last_day, aes(x = reorder(factor(state),cases), y = cases)) + 
         geom_col() + coord_flip() + labs(x="",y="confirmed cases")
1
votes

If we need a barplot, it is easier

barplot(with(last_day, tapply(cases, state, FUN = sum)))

Or with ggplot

library(ggplot2)
ggplot(last_day, aes(x = state, y = cases, fill = state)) + 
      geom_col()