Creating a map plot in R using plot_usmap package

4.3k views Asked by At

I have a dataset containing different states, zip codes, and claim counts each in separate columns. I am trying to create a plot to show the total claim count according to zip codes for the state of MA.

Dataset: enter image description here

I used this to filter by MA:

MA_medicare <- medicare %>%
  filter(medicare$NPPES.Provider.State == "MA")

I then used this to set the fips code for plot_usmap:

MA_medicare$NPPES.Provider.State <- fips(MA_medicare$NPPES.Provider.State)
setnames(MA_medicare, old=c("NPPES.Provider.State"), new=c("fips"))

And last tried to graph (not sure why this doesn't work):

plot_usmap(data = MA_medicare, values= c("Total.Claim.Count", "NPPES.Provider.Zip.Code"), include = c("MA")) + scale_fill_continuous(low= "white", high= "red") + theme(legend.position = "right") 

Error: Aesthetics must be either length 1 or the same as the data (4350838): fill

1

There are 1 answers

3
Paolo On

I'm the developer of usmap. plot_usmap only accepts one column of values for plotting so you're probably looking for the following:

plot_usmap(data = MA_medicare, values = "Total.Claim.Count", include = c("MA"))

However, your data is by zip code, and currently usmap doesn't support zip code maps (only state and county level maps). It uses the FIPS column to assign colors to states/counties on the map. Since you defined the FIPS codes by state, you'll just get the entire state of Massachusetts filled in with one solid color.