rusmap

Plotting data frame in R using usmap library


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


Solution

  • 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