celilati
celilati

Reputation: 165

R - tmap: Add thick border and transform counties with NAs to diagonal stripes

I am trying to recreate the following maps in R using tmap:

First Map: First Map

Second Map: Second Map

My questions are:

  1. How to transform the counties with NAs from grey (which I think is the default in tmap) to diagonal stripes that is shown in the first map?

  2. How to add a thick black border showing the whole NC state like in the second map?

Currently my R code is as follows:

tm_shape(dataframe) +

tm_polygons("variable1", id = "county", palette = "Greens", border.col = "black") +

tm_layout(panel.labels = c("ABCD"), legend.position = c("left"))

Note: The border.col = "black" colors each county border to black; however I would like to have a thicker border for the whole state.

Thank you!

Upvotes: 1

Views: 1693

Answers (1)

Susan Switzer
Susan Switzer

Reputation: 1922

Install and load the tigris package

    library(tigris)

Obtain a spatial object containing all state shapes

    state <- tigris::states() 

subset the SpatialPolygonsDataFrame to access the shape of NC

    NC <- state[state@data$NAME == "North Carolina", ]

Obtain a spatial object containing all counties in NC

    ncCounties <- counties("North Carolina", cb = TRUE)

Use the qtm(), tm_shape() and tm_borders() functions

    library(tmap)
    qtm(ncCounties, 
        fill = NULL, 
        fillCol = "GEOID",
        borders = "#515151", )+
   tm_layout(frame = FALSE)+
   tm_shape(NC)+
   tm_borders(lwd = 3, 
         col = "black")

enter image description here

Upvotes: 1

Related Questions