3

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

nathan
  • 191
  • 3
  • 11
  • 5
    What does "doesn't work" mean exactly? Do you get an error of some sort? What package does the `plot_usmap` function come from (that's not a base R function). It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 15 '18 at 22:19
  • @MrFlick I included more information now. The package I'm using is "usmap" and I'm the error im getting is: Error: Aesthetics must be either length 1 or the same as the data (4350838): fill – nathan Nov 15 '18 at 23:54
  • @www I apologize I was trying to reproduce the data set. I added a picture now of some of the rows and the columns I am trying to use. – nathan Nov 16 '18 at 01:11
  • 1
    Thanks for the update. It is a good idea to use `dput` like earlier you did with the `iris` data. Screenshot is a terrible way to share data because it is unlikely that someone is willing to type all the text based on your picture. – www Nov 16 '18 at 01:16

1 Answers1

2

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.

Paolo
  • 3,825
  • 4
  • 25
  • 41
  • 1
    Thanks for this great package Paolo. Its very useful when in an environment where you cant install `geos` and `gdal`. Im trying to map both state and county boundaries on the same map, is there a way to do this using `plot_usmap`? I tried connecting two separate plots using the canonical `+` from ggplot2 but no luck... Thanks! – Cyrus Mohammadian Jan 22 '20 at 00:58
  • 1
    @CyrusMohammadian unfortunately that's not currently a feature built into `usmap`. However, there is a workaround described here: https://github.com/pdil/usmap/issues/26. It will hopefully be in the next release so the workaround won't be needed (but I don't have a timeline for it at this time) – Paolo Feb 03 '20 at 14:16
  • 1
    Thank you for this amazing package, Paolo! Well done. – ColorStatistics Mar 28 '21 at 16:51