在 ggplot2 地图中仅绘制外边界 / geom_polygon
Plot only outer border in ggplot2 map / geom_polygon
我只想绘制苏丹 this data set. The data set plot_data
that you find under this link is a fortified data set from a shapefile from PRIO GRID 中 ethnic
变量的外边界。
我当前的代码如下所示:
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()
这给了我以下输出:
但是,我想删除指定民族聚居区的所有内线,只绘制该地区的外边界。
我没有找到 的解决方案。
我可能需要合并单元格 在我强化 shapefile 之前;不过强化之后说不定还有办法。我试图删除重复的坐标,但这没有用。任何建议将不胜感激。谢谢。
基本思路很简单:您需要将填充和颜色映射到整个地块的美感,然后将颜色映射到较小多边形的美感,这样您就可以用相同的颜色为较小的多边形着色作为更大的多边形。这就是摆脱较小多边形的内线的原因。然后我在较小多边形的美学之外添加了尺寸,以便将红线的宽度增加到可感知的水平。
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()
我的会话信息:
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
更新: OP 在使用我发布的代码生成相同的情节时遇到了麻烦。我的一个朋友使用我在此处提供的相同代码在 Windows 系统(我在 El Capitan)上重现了情节,他得到了相同的结果(如下)。
我只想绘制苏丹 this data set. The data set plot_data
that you find under this link is a fortified data set from a shapefile from PRIO GRID 中 ethnic
变量的外边界。
我当前的代码如下所示:
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()
这给了我以下输出:
但是,我想删除指定民族聚居区的所有内线,只绘制该地区的外边界。
我没有找到
我可能需要合并单元格 在我强化 shapefile 之前;不过强化之后说不定还有办法。我试图删除重复的坐标,但这没有用。任何建议将不胜感激。谢谢。
基本思路很简单:您需要将填充和颜色映射到整个地块的美感,然后将颜色映射到较小多边形的美感,这样您就可以用相同的颜色为较小的多边形着色作为更大的多边形。这就是摆脱较小多边形的内线的原因。然后我在较小多边形的美学之外添加了尺寸,以便将红线的宽度增加到可感知的水平。
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()
我的会话信息:
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
更新: OP 在使用我发布的代码生成相同的情节时遇到了麻烦。我的一个朋友使用我在此处提供的相同代码在 Windows 系统(我在 El Capitan)上重现了情节,他得到了相同的结果(如下)。