Felix Felix - 4 months ago 43
R Question

Plot only outer border in ggplot2 map / geom_polygon

I would like to plot only the outer boundaries of the

variable in this data set. The data set
that you find under this link is a fortified data set from a shapefile from PRIO GRID for Sudan.

My current code looks like this:

plot_data <- load("plot_data.rdata")

aes(x= long,
y = lat,
group = id)) +
geom_polygon() +
geom_polygon(data = plot_data %>%
filter(!is.na(ethnic)) %>% # subset data with ethnicity only
aes(color = ethnic)) +

This gives me the following output:

enter image description here

However, I would like to remove all interior lines of the settlement area of the specified ethnic group and plot only the outer border of the area.

I didn't get the solution for this similar problem to work.

I might need to combine the cells before I fortify the shapefile; but maybe there is a way after fortifying. I've tried to remove duplicate coordinates, but that didn't work. Any suggestions would be greatly appreciated. Thanks.


The basic idea is simple: you need to map fill and color to the aesthetics of the whole plot and then just map color to the aesthetics of the smaller polygon, that way you can color the smaller polygon with the same color as that of the larger polygon. That's what gets rid of the inside lines of the smaller polygon. Then I added size outside of the aesthetics of the smaller polygon in order to increase the width of the red line to a perceptible level.

plot_data <- load("plot_data.rdata")   


df<-plot_data %>% 
  filter(!is.na(ethnic)) %>% # subset data with ethnicity only

       aes(x= long, y = lat, group = id,fill="", color="")) + 
  geom_polygon() + 
  geom_polygon(data = df, 
               aes(color = ethnic), size=1) + 
  geom_polygon(data=df, aes(x= long, y = lat, group = id))+
  scale_fill_manual(values="black", guide=F)+
               labels=c("","Shaygiyya, Ja'aliyyin and Danagla (Arab)"),
               breaks = c("NA","Shaygiyya, Ja'aliyyin and Danagla (Arab)")) +

enter image description here

My session info:

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7        XML_3.98-1.4       proj4_1.0-8        bitops_1.0-6      
 [5] MASS_7.3-45        grid_3.3.1         plyr_1.8.3         gtable_0.2.0      
 [9] scales_0.4.0       KernSmooth_2.23-15 ggplot2_2.1.0      ash_1.0-15        
[13] RColorBrewer_1.1-2 RJSONIO_1.3-0      tools_3.3.1        RSelenium_1.4.2   
[17] munsell_0.4.3      RCurl_1.95-4.8     maps_3.1.0         colorspace_1.2-6  
[21] caTools_1.17.1 

UPDATE: The OP is having troubling producing the same plot using the code I posted. I had a friend of mine reproduce the plot on a Windows system (I'm on El Capitan) using the same code I supplied here and he got the same results (below).

enter image description here