2

I want to plot a map of the following data, dt_plot, which has 2 unique counties in it.

fr      long        lat       group order    region       subregion    polyname
10   -121.031609 48.3060722  2894   85063  washington    chelan       washington,chelan
12   -121.054520 48.3289909  2894   85064  washington    chelan       washington,chelan
22   -121.054520 48.3461800  2894   85065  washington    chelan       washington,chelan
23   -121.037331 48.3519096  2894   85066  washington    snohomish    washington,chelan
34   -121.025871 48.3633690  2894   85067  washington    snohomish    washington,chelan
1    -121.065979 48.3977432  2894   85068  washington    snohomish    washington,chelan
5    -121.134743 47.9680252  2924   86403  washington    snohomish    washington,snohomish

I try the following code from (here):

ggplot(dt_plot, aes(long, lat, group = group)) + 
geom_polygon(aes(fill = fr), colour = rgb(1, 1, 1, 0.2))  +
coord_quickmap()

When I plot it, only the counties in the data set shows up. I want the plot to be laid over all USA. How can I do that? I can add missing sub-regions, however, since I use the column fr for color coding it, if I do that, then the legend will be messed up.

Community
  • 1
  • 1
OverFlow Police
  • 861
  • 6
  • 23
  • 2
    If you just want an outline of the USA download the outline polygon and add it as another layer (`+ geom_sf(data = usa)`...). If you want a basemap see this post: https://stackoverflow.com/questions/47749078/how-to-put-a-geom-sf-produced-map-on-top-of-a-ggmap-produced-raster – Phil Apr 16 '19 at 22:29
  • how dumb am I? you can turn your comment to a solution! I did it like so: cnty <- map_data("county") # Load the county data from the maps package cnty2 <- cnty %>% mutate(polyname = paste(region, subregion,sep=",")) %>% left_join(county.fips, by="polyname") geom_polygon(data = cnty2) Did not wanna go through headache of dealing with sf and geometry stuff. – OverFlow Police Apr 16 '19 at 22:37
  • Happens to the best of us! Write up your answer and I'll upvote it (and you can self-accept) – Phil Apr 17 '19 at 07:00

1 Answers1

0
cnty <- map_data("county") # Load the county data from the maps package 
cnty2<- cnty %>% 
         mutate(polyname = paste(region, subregion,sep=",")) %>% 
         left_join(county.fips, by="polyname") 

and add geom_polygon(data = cnty2) as another layer:

ggplot(dt_plot, aes(long, lat, group = group)) + 
geom_polygon(data = cnty2, fill="lightgrey") + 
geom_polygon(aes(fill = freq), colour = rgb(1, 1, 1, 0.2))  +
geom_text(data=target_annotation, aes(long, lat, label = subregion)) + 
coord_quickmap()
OverFlow Police
  • 861
  • 6
  • 23