I'd like to make a map in R of the continental U.S. (using the maps package), where one state is filled in in black (CA) and one state (TX) is filled in with cross-hatches or some other pattern. The rest of the states should have black borders with no fill. I'm sure there's a simple solution, but I haven't found it yet.
-
2What would help you getting answers are examples of what you have tried and the things that have failed. – simonmorley Jan 28 '13 at 23:40
3 Answers
I recently had the same issue but had too many states to hatch by hand with polygon. It seems that just adding density=10 to the map for a particular state will fill it with hatched bars.
So for the example here try:
map("state")
map("state", add = TRUE, region = c("texas"), density=10, fill=TRUE)
map("state", add = TRUE, region = c("california"), fill=TRUE)
I got the idea here: Fill Geospatial polygons with pattern - R

- 1
- 1

- 41
- 2
-
-
Also works if you have a map shapefile and use plot(map, add=TRUE, density=10) – Iain S Sep 21 '18 at 13:45
I can get this to work by saving the state data and replotting. A little bit of polygon
fiddling is required so that the outline of the state polygon draws in the correct order without doubling back on itself. How much fiddling is required probably depends on which state polygon you want. In this instance, I just had to reverse the Texas/New Mexico border.
library(maps)
map("state")
tempplot <- map("state", add = TRUE, region = c("texas"),plot=FALSE)
# fix the border with new mexico to draw it in the correct order
tempplot$x[829:861] <- tempplot$x[861:829]
tempplot$y[829:861] <- tempplot$y[861:829]
polygon(
na.omit(tempplot$x),
na.omit(tempplot$y),
border=1,
density=10
)
map("state", add = TRUE, region = c("california"), fill=TRUE, col="black")

- 91,185
- 12
- 128
- 188
-
This is very helpful. Thanks! Just one more request: What if I wanted to use cross-hatching for multiple states? Say, Texas, Florida, Nevada, Georgia, New Mexico, Tennessee, North Carolina, and New York? Sorry if this is an obvious question -- I don't have much experience using the maps package. – user2019886 Jan 29 '13 at 04:54
-
@user2019886 - you would need to repeat the part of the above code that starts with `tempplot <- ...` and ends with the `polygon()` function for each state you want to hatch. As I say in the answer though, there is a bit of manual fiddling with each polygon to make sure it makes a nice closed shape. Without going through each state you mention, I couldn't say how easy or hard this would be to get working. – thelatemail Jan 29 '13 at 05:08
-
Where do you obtain the coordinates for the states (e.g., 829:861) so that you can do the manual fiddling with each polygon? – user2019886 Jan 29 '13 at 14:35
This should be a good start.
library(maps)
map('state')
map("state", col = "black", fill = TRUE, add = TRUE, region = c('california'))
map("state", col = "red", fill = TRUE, add = TRUE, region = c('texas'))
PS : cross-hatched fill patterns is generally disapproved. So I fill Texas with red here.
EDIT
One opition is to play with lty and lwd to differentiate between states. It looks tricky but the result is clear.
map("state", lty = 3)
map("state", col = "black", fill = TRUE, add = TRUE, region = c('california'))
map("state", lty=1,lwd = 3, fill = FALSE, add = TRUE, region = c('texas'))

- 119,832
- 17
- 199
- 261
-
I agree that cross-hatching is sub-optimal, but our book press requires that the map use only black and white, so colors are not an option. – user2019886 Jan 29 '13 at 04:51
-