3

I have a choropleth map that represents the number of states being affected by a storm event. This is shown in the image below. I want to lay a transparent line or polygon over these states to represent the storm area. How do I do this?

enter image description here

Here is the code I'm using for ggplot:

ggplot() + geom_polygon(data = final.plot, aes(x = long, y = lat, group = group, fill = factor(T3)), color = "grey", size = 0.3) +
          coord_map("conic", lat0 = 30, xlim = c(-121, -72), ylim = c(25, 50)) + theme(axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), legend.position = "bottom") +
          geom_point(data=s1.lat.long, aes(x=long, y=lat), color="black", size=1)

Here is a link to the whole R code and shape files.

EDIT: I would like it to look like this mock image below, whereby a band spreads all the way across the US, representing geomagnetic storm activity. This band can then be made fairly transparent.

enter image description here

Really, this is a crude way of reproducing this image:

enter image description here

1 Answer 1

2

Edit this snippet into your code:

library(rgeos) # to use dissolve polygons
library(broom) # to replace fortify() with tidy().

# Select Affected states based on $T3. Subset and merge to one polygon.
statesAffectedList <- as.character(unique(final.plot$NAME_1[final.plot$T3 == "State Affected"]))
states.shp$STATE_NAME <- as.character(states.shp$STATE_NAME)
statesAffectedSHP <- gUnaryUnion(states.shp[states.shp$STATE_NAME %in% statesAffectedList, ])

stormArea <- tidy(statesAffectedSHP)  # tidy() replaces fortify(). See ?fortify for details

Later add stormArea to the plot using the following:

geom_polygon(data = stormArea, aes(x = long, y = lat, group = group), color = "black", alpha = 0, linetype = 1)

Note that you can change alpha parameter to add a fill effect to your polygon, and you can also control the border with the linetype. Following are the complete and adjusted plotting code and an image with the result.

ggplot() + geom_polygon(data = final.plot, aes(x = long, y = lat, group = group, fill = factor(T3)), color = "grey", size = 0.3) + coord_map("conic", lat0 = 30, xlim = c(-121, -72), ylim = c(25, 50)) +
theme(axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), legend.position = "bottom") +
geom_point(data=s1.lat.long, aes(x=long, y=lat), color="black", size=1) + 
geom_polygon(data = stormArea, aes(x = long, y =  lat, group = group), color = "black", alpha = 0, linetype = 1) 

StormArea within the black border

2
  • That's great dof1985, I got the code working, as you can see from the edits I made to the question. However, do you think you could help me make it more like a storm footprint which spreads right across the image, similar to those in the example images that I posted above? Thanks, Ed Jul 26, 2016 at 20:29
  • I guess you should have some data that enclose this so-called storm area. I just selected states-affected, but it seems that you want this area to cover a bigger area. If you have a shpaefile of this area you can load it as well and plot it on top with alpha set to let's say 0.35 to get it transperant
    – dof1985
    Jul 27, 2016 at 6:31

Your Answer

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

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