Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a map plot in R using plot_usmap package

Tags:

r

filter

usmap

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

like image 687
nathan Avatar asked Dec 20 '25 00:12

nathan


1 Answers

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.

like image 132
Paolo Avatar answered Dec 22 '25 19:12

Paolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!