简体   繁体   English

仅在 ggplot2 地图 / geom_polygon 中绘制外边框

[英]Plot only outer border in ggplot2 map / geom_polygon

I would like to plot only the outer boundaries of the ethnic variable in this data set .我只想在这个数据集中绘制ethnic变量的外边界。 The data set plot_data that you find under this link is a fortified data set from a shapefile from PRIO GRID for Sudan.您在此链接下找到的数据集plot_data是来自苏丹PRIO GRID的 shapefile 的强化数据集。

My current code looks like this:我当前的代码如下所示:

plot_data <- load("plot_data.rdata")

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

This gives me the following output:这给了我以下输出:

在此处输入图片说明

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;强化 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")   

library(ggplot2) 

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

ggplot(plot_data, 
       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)+
  scale_color_manual(name="ethnic",
               labels=c("","Shaygiyya, Ja'aliyyin and Danagla (Arab)"),
               values=c("black","red"), 
               breaks = c("NA","Shaygiyya, Ja'aliyyin and Danagla (Arab)")) +
  coord_equal()

在此处输入图片说明

My session info:我的会话信息:

sessionInfo()
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)

locale:
[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.更新: OP 在使用我发布的代码生成相同的图时遇到了麻烦。 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).我的一个朋友使用我在此处提供的相同代码在 Windows 系统(我在 El Capitan)上重现了该图,他得到了相同的结果(如下)。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM