0

I am looking to shade specific counties on map.
just to begin working on this, I took this base code here ( suggested in several posts with no error mentioned ) .for example :[https://stackoverflow.com/questions/33129917/shading-counties-using-fips-code-in-r-map

library(maps)

library(dplyr)

data(county.fips)

## Set up fake df_pop_county data frame
df_pop_county <- data.frame(region=county.fips$fips)
df_pop_county$value <- county.fips$fips
y <- df_pop_county$value
df_pop_county$color <- gray(y / max(y))

## merge population data with county.fips to make sure color column is
## ordered correctly.
counties <- county.fips %>% left_join(df_pop_county, by=c('fips'='region'))
map("county", fill=TRUE, col=counties$color)

but when I run this ( or even similar map() related codes ) I get : Error in numeric(nrowz) : invalid 'length' argument in map() r
I appreciate any suggestion how to resolve this error.

Mathica
  • 25
  • 1
  • 6

1 Answers1

0

Your code doesn't run. Do you get the same error if you run this?

library(maps)
library(tidyr)
library(dplyr)

data(county.fips)

dat=separate(county.fips, polyname, sep= ",", into=c("state", "county"))
dat = dat %>% group_by(state) %>% mutate(grp=cur_group_id(), color=terrain.colors(49)[grp])
map("county", fill=TRUE, col=dat$color, boundary=FALSE)

enter image description here

Vons
  • 3,277
  • 2
  • 16
  • 19
  • Thanks for your answer. and YES. I get the same error : Error in numeric(nrowz) : invalid 'length' argument. I even try basic codes in rstudio help for map() and I get the same error. – Mathica Mar 16 '21 at 01:26
  • still same error. I tried your code on another laptop and it works. what could it be then the problem that I can not run these codes on my machine. – Mathica Mar 16 '21 at 01:41
  • What's it say when you run `Sys.getenv("R_MAP_DATA_DIR")` go to that directory and check if there is county.L, county.G, and county.N file in there – Vons Mar 16 '21 at 01:48
  • What's it say when you run Sys.getenv("R_MAP_DATA_DIR") go to that directory and check if there is county.L, county.G, and county.N file in there. Replace them with these three files from here: https://filebin.net/xoxz6kfa29gdb2ly and see if it works – Vons Mar 16 '21 at 01:55
  • I have all those three county.L county.G and county.N there. – Mathica Mar 16 '21 at 01:55
  • 1
    WOWWW. it worked!!! I was really struggling! thank you. Now I ran your code and I got the same map as yours. – Mathica Mar 16 '21 at 01:57