简体   繁体   English

R 传单 addPolygons - 如何孵化多边形?

[英]R leaflet addPolygons - how to hatch polygons?

With leaflet() and addPolygons() , it is possible to color polygons (USA states in the example below) according to a specific variable.使用addPolygons() leaflet()addPolygons() ,可以根据特定变量为多边形(以下示例中的美国州)着色。

Q/ Is there a way to hatch polygons in order to add a second information within the map ?问/有没有办法孵化多边形以在地图中添加第二个信息?

Color give a first information (cluster belonging), I would like to add a second information (states with 'New' in their name for instance).颜色给出了第一个信息(属于集群),我想添加第二个信息(例如,名称中带有“新”的状态)。 It could be very useful to visualize the both information at the same time.同时可视化这两种信息可能非常有用。

library(rgdal)

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
states <- readOGR(
  dsn   = "2.Data/shp/cb_2013_us_state_20m.shp", 
  layer = "cb_2013_us_state_20m", 
  GDAL1_integer64_policy = T
)

sample <- states %>% 
  subset(STUSPS %in% c("CT","ME","MA","NH","RI","VT","NY","NJ","PA"))

MaPalette1 <- colorFactor(c('red', 'blue', 'green', 'grey', 'black', 'pink', 'orange', 'yellow', 'purple', 'white'), 
                          sample@data$STATEFP)

leaflet(sample) %>%
  addPolygons(
    color       = 'black', 
    weight      = 1,
    fillOpacity = 1,
    fillColor   = ~ MaPalette1(STATEFP)
)

在此处输入图片说明

I tried to color the border of the polygons, but as there already are a lot of colors in the map, it is not easy to visualize.我尝试为多边形的边界着色,但由于地图中已经有很多颜色,因此不容易可视化。

I also tested addLayersControl(), but I really want to visualize information on the same layer, and superposition of two color layers create new colors, the information is not understandable.我也测试了addLayersControl(),但是我真的很想在同一层上可视化信息,并且两个颜色层的叠加创建新的颜色,信息无法理解。

Thank in advance for your help.预先感谢您的帮助。 When I said 'hatching', I think something like that:当我说“孵化”时,我认为是这样的:

在此处输入图片说明

Since your question is a few months old, please let me know if you found another solution, and share it.由于您的问题是几个月前的问题,如果您找到了其他解决方案,请告诉我,并分享它。 The package HatchedPolygons should do what you're looking for. HatchedPolygons 包应该可以满足您的需求。 Below is a working example with the same data that you tried with:下面是一个与您尝试使用的数据相同的工作示例:

devtools::install_github("statnmap/HatchedPolygons")
library(diplyr)

# From https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html
states <- rgdal::readOGR(
  dsn   = "~/Downloads/cb_2013_us_state_20m/cb_2013_us_state_20m.shp", 
  layer = "cb_2013_us_state_20m", 
  GDAL1_integer64_policy = T
)

sample <- states %>% 
  subset(STUSPS %in% c("CT","ME","MA","NH","RI","VT","NY","NJ","PA"))

MaPalette1 <- colorFactor(c('red', 'blue', 'green', 'grey', 'black', 'pink', 'orange', 'yellow', 'purple', 'white'), sample@data$STATEFP)

# hatching
sample3 <- sample %>% 
  subset(STUSPS %in% c("NH", "NJ")) 
sample3.hatch <- HatchedPolygons::hatched.SpatialPolygons(sample3, density = c(6,4), angle = c(45, 135))

# plot
leaflet(sample) %>%
  addPolygons(
    color       = 'black', 
    weight      = 1,
    fillOpacity = 1,
    fillColor   = ~ MaPalette1(STATEFP)
  ) %>% 
  addPolylines(
    data = sample3.hatch,
    color = c("white", "red"),
    weight = 1.0
  )

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

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