aaa*_*aaa 3 nested r polygons fill ggplot2
我在一个独特的SpatialPolygonsDataFrame. 我希望用它们来绘制它们,ggplot2并且它们都需要在地图中可见,即覆盖较大的多边形必须在背景中。
我发现我可以通过alpha = 0在geom_polygon函数内使用来实现这一点,但是如何为每个多边形分配填充颜色?
这是我的代码示例,只有 2 个多边形:
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)
读取合并两个 shapefile 的 csv 文件,然后使用fortifyfrom转换为 data.frame maptools。
#read csv file shape_1_2.csv
shape_1_2 = read.csv('shape_1_2.csv', stringsAsFactors = FALSE)
#plot
ggplot() +
geom_polygon(data = shape_1_2, aes(x = long, y = lat, group = group),
colour = 'black', size = 1, linetype = 'solid', alpha = 0)
Run Code Online (Sandbox Code Playgroud)
和相对地图:
如何用颜色填充这两个多边形?
我试图添加fill='black'两者aes,geom_polygon但它不起作用。
谢谢
更新
很抱歉,但我意识到我的示例数据不包含嵌套多边形。
因此,根据https://gis.stackexchange.com/questions/280671/r-create-multipolygon-from-overlapping-polygons-using-sf-package从以下 data.frame 开始:
shape_df = data.frame(
lon = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2, 2, 0.8, 1, 1, 2, 2, 1, 1),
lat = c(0, 0, 1, 1.5, 0, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 0, 0, 1, 1, 0),
var = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3 ,3 ,3 ,3 ,3, 4 ,4 ,4, 4, 4)
)
Run Code Online (Sandbox Code Playgroud)
还有我的绘图代码(带有alpha=0):
ggplot() +
geom_polygon(data = shape_df, aes(x = lon, y = lat, group = var),
colour = 'black', size = 1, linetype = 'solid', alpha = 0)
Run Code Online (Sandbox Code Playgroud)
使用相对地图:
如何用一种或最多 4 种颜色填充地图中存在的不同区域,以便较大的多边形保留在较小的背景中?
如果您sf使用st_area来执行此操作,则可以使用来获取每个多边形的面积(面积对于未投影的玩具数据没有多大意义,但对于实际形状会有意义),然后根据面积对多边形进行排序。这样,ggplot将按 ID 顺序创建多边形。要使用geom_sf,您需要github dev 版本ggplot2,尽管它已添加到下个月(2018 年 7 月)的下一个 CRAN 版本中。
首先从数据创建一个简单的特征集合。在这种情况下,我必须summarise(do_union = F)按照正确的顺序将每个系列的点组成一个多边形(根据最近的问题),然后计算每个点的面积。
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
shape_df <- data.frame(
lon = c(0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2, 2, 0.8, 1, 1, 2, 2, 1, 1),
lat = c(0, 0, 1, 1.5, 0, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 0, 0, 1, 1, 0),
var = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3 ,3 ,3 ,3 ,3, 4 ,4 ,4, 4, 4)
)
shape_areas <- shape_df %>%
st_as_sf(coords = c("lon", "lat")) %>%
group_by(var) %>%
summarise(do_union = F) %>%
st_cast("POLYGON") %>%
st_cast("MULTIPOLYGON") %>%
mutate(area = st_area(geometry)) %>%
mutate(var = as.factor(var))
shape_areas
#> Simple feature collection with 4 features and 3 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: 0 ymin: 0 xmax: 2 ymax: 2
#> epsg (SRID): NA
#> proj4string: NA
#> var do_union area geometry
#> 1 1 FALSE 1.25 MULTIPOLYGON (((0 0, 1 0, 1...
#> 2 2 FALSE 1.00 MULTIPOLYGON (((0 1, 1 1, 1...
#> 3 3 FALSE 1.10 MULTIPOLYGON (((1 1, 2 1, 2...
#> 4 4 FALSE 1.00 MULTIPOLYGON (((1 0, 2 0, 2...
Run Code Online (Sandbox Code Playgroud)
如果我此时绘图,则该区域与绘图顺序无关;它只是按var数字排序:
shape_areas %>%
ggplot() +
geom_sf(aes(fill = var), alpha = 0.9)
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用通过减少面积forcats::fct_reorder来重新排序var作为一个因素,多边形将按顺序绘制,最大的多边形在底部,较小的多边形在顶部分层。编辑:正如@SeGa 在下面指出的那样,这最初是将更大的形状放在上面。使用-area或desc(area)进行降序排序。
shape_areas %>%
mutate(var = var %>% fct_reorder(-area)) %>%
ggplot() +
geom_sf(aes(fill = var), alpha = 0.9)
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.0)于2018年 6 月 30 日创建。
| 归档时间: |
|
| 查看次数: |
2420 次 |
| 最近记录: |