3

I am relatively new to using R and I am trying to create maps of US states using data to outline and colour certain areas. I am trying to display a state and its counties outlined in black. On top of this, I would like to create thick red borders around groups of counties and colour-fill some counties based on some data I have.

Essentially I want to combine these two images:

I would like to changed this map to outlines of the coloured areas, so -for example - there would be a red border around everything blue.

Then I would like to fill the map above like this

This is the code I have written so far attempting this task:

# Maping IA, plan 74406IA0010001

# Importing data 
library(ggplot2) 
library(ggmap) 
library(maps) 
library(mapdata) 
library(stringr) 
library(plyr) 
library(dplyr)

setwd("/Users/erinmay/Desktop/WL_RA/marketplace2/data")

county <- map_data("county") 
plan <- read.csv("IA_2017.csv")

# Using subset

iowa <- subset(county, region=="iowa") #county point files for iowa

# Merging in map data

countyplan <- merge(x=iowa, y=plan, by=c("region","subregion"), all.x=TRUE)

countyplan <- countyplan[order(countyplan$chosen_plan),]

# Creating map  

final <- ggplot(data=countyplan) + 
           geom_path(aes(x=long,y=lat,group=RatingArea),colour='black') +  
           geom_polygon(aes(x=long,y=lat,group=group,fill=chosen_plan)) + 
           coord_map() + coord_fixed(1.3)

ggsave(final,height=6,width=10,unit='in',file='iowa.pdf')

Thank you in advanced for your help!

Here is the data: https://www.dropbox.com/s/x8x2l50dvmg0lsb/QHP_IA_2017.csv?dl=0

2
  • Can we have some sample data? It would help with testing solutions. See mcve & reproducible example in R.
    – Z.Lin
    Aug 29, 2017 at 1:02
  • The link to example data is broken. Does anyone have the data or a sample at least? Jun 4, 2018 at 19:36

1 Answer 1

1

Edited answer based on OP's clarification to only colour the outer borders of each rating area:

As far as I understand it, all polygons are created equal where ggplot is concerned. Hence if it has to colour the outline of a polygon, it will do so for all sides, regardless whether two adjacent polygons belong to the same rating area.

You'll have the dissolve polygons in the same planning area, before fortifying them into a data frame. (Note: you can convert the existing data frame back to polygon, but it's probably easier to get polygon data from the original data source.)

library(maps); library(dplyr); library(tidyr); library(maptools); library(rgeos)

# get map data
county_map <- map("county", fill = T, plot = FALSE)

# create mapping table between county names & rating areas
county_map_match <- data.frame(name = county_map$names) %>%
  separate(name, c("region", "subregion"), sep = ",", remove = FALSE) %>%
  left_join(plan %>% select(region, subregion, RatingArea))
rownames(county_map_match) <- county_map_match$name

# convert map to SpatialPolygon, then join with mapping table for SpatialPolygonDataFrame
county_map <- map2SpatialPolygons(county_map, IDs = county_map$names)
county_map <- SpatialPolygonsDataFrame(county_map, county_map_match)

# remove invalidities in the county map
gIsValid(county_map) #returns FALSE: there are invalid self-intersecting geometries in the polygons, which will cause problems
county_map <- gBuffer(county_map, byid = TRUE, width = 0)
gIsValid(county_map) #returns TRUE

# dissolve county map by rating area & fortify to data frame
area_map <- unionSpatialPolygons(county_map, IDs = county_map$RatingArea)
area_map <- fortify(area_map)
area_map$group <- gsub(".1", "", x= area_map$group, fixed = T)

Once you obtain the data frame version for rating areas, you can incorporate it into the ggplot:

ggplot(countyplan,
       aes(x=long,y=lat, group = group, fill = chosen_plan)) + 
  geom_polygon(size = 0.5, colour = "black") +
  geom_polygon(data = area_map, 
            aes(x=long, y=lat, group = group, colour = group), 
            fill = NA, size = 2) +
  scale_fill_manual(name = "Chosen Plan", values = c("darksalmon"), na.value = "grey") +
  scale_color_discrete(name = "Rating Area") +
  coord_map() + coord_fixed(1.3)

edited ggplot with outer borders coloured

You can get nicer color palettes from the RColorBrewer package & use them in the scale_XX_brewer() call, if you like. Names for individual colors can be referenced here: http://sape.inf.usi.ch/quick-reference/ggplot2/colour

3
  • Almost! This is very helpful, so thank you very much. Is there a way to change the interior lines of a Rating Area to black and just leave the outer borders of the Rating Area coloured?
    – Erin
    Aug 29, 2017 at 16:01
  • @Erin Edited my solution.
    – Z.Lin
    Aug 30, 2017 at 4:11
  • This worked for me. For anyone trying to map their dissolved polygons onto a usmap() (which uses UTM coordinates and AEA projection) you need to run this before plotting: area_map <- usmap_transform(data=area_map, input_names=c("long", "lat")) Then plot the data with plot_usmap() + geom_polygon(data=area_map, aes(x=x, y=y, group=group, color=group), etc.
    – BonnieM
    Nov 29, 2022 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.