1

I'm new to R and I'm trying to plot a data frame of county values using the usmap library.

I have a dataset containing all the FIPS (county codes) for a particular region and the data (deaths) that I want to show on the map.

This is my first R script so what I'm trying to accomplish is likely pretty easy and I just have no idea what I'm doing.

I'm pretty sure the error I'm receiving is because I haven't specified any kind of coloring to apply to the data? I'm unsure.

Here's my code - note that I'm trying to initially just plot one frame of data (a particular date):

library(usmap)
library(ggplot2)
library(RColorBrewer)

#set working directory
setwd("C:/Users/Name/Documents/RScripts/")

#input data from file separated by  commas
usa.dat <- read.csv("covid.csv", header = T)

#Start at 1/22/2020
#End at 10/8/2021

plot_usmap(regions = "counties",
           data=usa.dat$countyFIPS,
           values=usa.dat$X1.22.2020,
           ) +
theme(panel.background = element_rect(color = "black", fill = "black"))

Here's the data:

Data

The error I'm getting is Error in if (is.null(geom_args[["fill"]]) & nrow(data) == 0) { : argument is of length zero

When I remove the data/values lines from the function, I get a map that looks like this:

Map

Any help is greatly appreciated!

Ideally, I'd like to animate each frame of the data with color scales; if you guys can help me with that, I'd appreciate it!

Edit:

Okay so, I've been working on this for awhile and I managed to get it working. I have no idea how to update the color scales/gradients that are used, however.

I got it to loop through the data and save a bunch of plots, so that's pretty awesome! Just need to figure out how to change the colors/scales if anyone can help!

Update

user1274820
  • 7,786
  • 3
  • 37
  • 74
  • 1
    I would review `?plot_usmap` carefully, since it spells out what you need specifically. For example, you may want to have your column of fips codes just called `fips`, pass the whole data.frame as data, and indicate the column for values, such as: `plot_usmap(regions = "counties", data = usa.dat, values="X1.22.2020")` and this would work for a particular date... – Ben Oct 11 '21 at 21:31
  • I've made some progress since initially posting this. Now I'm really just confused on how to modify the themes and save the actual images (and do things like set the label titles) – user1274820 Oct 11 '21 at 21:36
  • I'll update the question in a sec with what I have now – user1274820 Oct 11 '21 at 21:54
  • @Ben see my edits – user1274820 Oct 11 '21 at 21:58
  • 1
    would also learn more about `ggplot2` (`plot_usmap` returns a `ggplot` object)...so you can add layers and pass aesthetics easily...so in your case you can use `scale_fill_continuous` to change the limits and gradients of the scale...try adding something like: `scale_fill_continuous(low = "white", high ="green", name = "Legend", limits = c(0,300))` and change colors and limit values to meet your needs... – Ben Oct 11 '21 at 23:01
  • @Ben is there a good way to do multiple breaks? – user1274820 Oct 11 '21 at 23:51
  • 1
    I'm not sure what you have in mind - but in might be worthwhile to post a new question focused on this...and when you post the question, use `dput` to share data (instead of a screenshot), and include the code you've tried so far...good luck! – Ben Oct 11 '21 at 23:54
  • @Ben if you want to post your first comment as an answer, I'll accept it – user1274820 Oct 11 '21 at 23:57

1 Answers1

3

I got it figured out (with some help from Ben!)

library(usmap)
library(ggplot2)
library(viridis)
library(RColorBrewer)
library(stringr)
library(stringi)

#set working directory
setwd("C:/Users/Tyrael/Documents/RScripts/")

#input data from file separated by  commas
usa.dat <- read.csv("ccovid.csv", header = T)

for (i in 2:ncol(usa.dat)) {
    da <- data.frame(fips=usa.dat$countyFIPS,val=usa.dat[,i])
    da$val <- cut(da$val,breaks=c(0,100,500,1000,5000,20000,30000),labels=c("1-100","100-500","500-1K","1K-5K","5K-20K","20K-30K"))
    theDate <- substr(names(usa.dat)[i],2,100)
    plot_usmap(regions = "counties",
               data=da,
               values="val"
    ) + 
    labs(title=paste("Covid-19 Deaths ",str_replace_all(theDate,"\\.","/"),sep='')) +
    scale_fill_viridis(name="Deaths",discrete=TRUE,na.translate=F) +
    theme(panel.background = element_rect(color = "#101010", fill = "#101010"))
    ggsave(paste(sprintf("%03d",i),".png",sep=''))
}

This splits everything up into a legend that looks like this:

Map

The files are output in sequential order and, as a bonus, I'll show how to combine in ffmpeg:

ffmpeg -framerate 15 -i "C:\Users\Tyrael\Documents\RScripts\vpublish\%03d.png" -codec copy out.mkv

And to get it into .mp4 format:

ffmpeg -i out.mkv -codec copy out.mp4

Results: https://www.youtube.com/watch?v=-z3LL5j__es

user1274820
  • 7,786
  • 3
  • 37
  • 74