【问题标题】:Fill Geospatial polygons with pattern - R用图案填充地理空间多边形 - R
【发布时间】:2014-02-10 12:39:37
【问题描述】:

我有一张波斯尼亚的地图,上面有根据居住在那里的多数族裔而不同颜色的城市。 但是,我想使用不同的图案而不是颜色(或灰度),因为它将以黑白打印。
我已经搜索过,但找不到方法。有谁知道如何做到这一点? Link to shapefile

到目前为止,这是我的代码:

library(RColorBrewer)
library(maptools)
library(rgdal)
library(rgeos)
library(ggplot2)
library(gridExtra)

setwd("path")

bosnia <- readOGR("path/to/file", "bosnia_analysis", 
                verbose = TRUE, stringsAsFactors = FALSE)

bosnia <- readShapePoly("path/to/bosnia_analysis.shp",proj4string=CRS("+proj=longlat +datum=WGS84"))
bosnia.df <- bosnia@data

serbs <- bosnia[bosnia$SEPRIORITY > bosnia$CRPRIORITY & bosnia$SEPRIORITY > bosnia$MOPRIORITY,]
croats <-  bosnia[bosnia$CRPRIORITY > bosnia$SEPRIORITY & bosnia$CRPRIORITY > bosnia$MOPRIORITY,]
moslems <- bosnia[bosnia$MOPRIORITY > bosnia$CRPRIORITY & bosnia$MOPRIORITY > bosnia$SEPRIORITY,]

p <- ggplot(bosnia, aes(x = long, y = lat, group = group)) + 
  geom_polygon(aes(x=long,y=lat,group=group), fill="white", colour="grey") +
  geom_polygon(data=serbs, aes(x=long,y=lat,group=group), fill="black", colour="grey") +
  geom_polygon(data=croats, aes(x=long,y=lat,group=group), fill="green", colour="grey") +
  geom_polygon(data=moslems, aes(x=long,y=lat,group=group), fill="red", colour="grey") +
  # Styling
  coord_map() +
  labs(x="Bosnia", y=" ") + 
  theme_bw() + 
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) + 
  theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) + 
  theme(panel.border = element_blank())

p

这给了我以下地图:

【问题讨论】:

标签: r ggplot2 gis


【解决方案1】:

放弃ggplot 以获得基本图形。虽然只有三个组,但我认为黑色、白色和中灰色都可以。

require(sp)
require(rgdal)

bosnia = readOGR(".","bosnia_analysis")
proj4string(bosnia)=CRS("+init=epsg:4326")

不要拆分成 3 个数据集,而是从三个 TRUE/FALSES 中创建一个新的分类变量:

serbs = bosnia$SEPRIORITY > bosnia$CRPRIORITY & bosnia$SEPRIORITY > bosnia$MOPRIORITY
croats =  bosnia$CRPRIORITY > bosnia$SEPRIORITY & bosnia$CRPRIORITY > bosnia$MOPRIORITY
moslems = bosnia$MOPRIORITY > bosnia$CRPRIORITY & bosnia$MOPRIORITY > bosnia$SEPRIORITY

bosnia$group=NA
bosnia$group[serbs]="Serb"
bosnia$group[croats]="Croat"
bosnia$group[moslems]="Moslem"
bosnia$group=factor(bosnia$group)

检查没有人属于一个以上的类别:

sum(serbs&&croats&&moslems) # should be zero

现在你可以得到一个漂亮的彩色图:

spplot(bosnia, "group")

但我看不出如何在不同的单声道样式中做到这一点,所以回到基础图形:

plot(bosnia,density=c(5,10,15)[bosnia$group], angle=c(0,45,90)[bosnia$group])

根据口味调整参数。您可以使用legend 用相同的参数做一个漂亮的图例。

【讨论】:

  • ggplot 中的 geom_sf 有没有办法做到这一点??
  • 没有。使用 as(x,"Spatial") 将您的 sf 对象转换为 sp 类,使用 library(sp) 并使用上述技术进行绘图。如果您想这样做,请向ggplot2 提交功能请求。
  • 明白了.. 感谢您的帮助!
【解决方案2】:

有点晚了,但这可能会有所帮助。您可以使用sf 创建一个网格并对其进行操作以提取特定点并将它们连接起来以创建新模式:

library(sf)

bosnia <- st_read("~/R/mapslib/OTROS/Bosnia")
st_crs(bosnia) <- 4326
serbs <-
  bosnia[bosnia$SEPRIORITY > bosnia$CRPRIORITY &
           bosnia$SEPRIORITY > bosnia$MOPRIORITY, ]
croats <-
  bosnia[bosnia$CRPRIORITY > bosnia$SEPRIORITY &
           bosnia$CRPRIORITY > bosnia$MOPRIORITY, ]
moslems <-
  bosnia[bosnia$MOPRIORITY > bosnia$CRPRIORITY &
           bosnia$MOPRIORITY > bosnia$SEPRIORITY, ]

ex = list(
  horizontal = c(1, 2),
  vertical = c(1, 4),
  left2right = c(2, 4),
  right2left = c(1, 3)
)
pattern <- function(x, size, pattern) {
  ex = list(
    horizontal = c(1, 2),
    vertical = c(1, 4),
    left2right = c(2, 4),
    right2left = c(1, 3)
  )
  fillgrid = st_make_grid(x, cellsize = size)
  endsf = lapply(1:length(fillgrid), function(j)
    sf::st_linestring(sf::st_coordinates(fillgrid[j])[ex[[pattern]], 1:2]))
  endsf = sf::st_sfc(endsf, crs = sf::st_crs(x))
  endsf = sf::st_intersection(endsf, x)
  endsf = endsf[sf::st_geometry_type(endsf)
                %in% c("LINESTRING", "MULTILINESTRING")]
  endsf = sf::st_line_merge(sf::st_union(endsf))
  return(endsf)
}

serbgrid = pattern(serbs, 0.05, "vertical")
moslgrid = pattern(moslems, 0.05, "left2right")
crogrid = pattern(croats, 0.05, "horizontal")


par(mar = c(0, 0, 0, 0))
plot(st_geometry(bosnia))
plot(serbgrid, add = T)
plot(moslgrid, add = T)
plot(crogrid, add = T)

请注意,这是我开发的一个函数的简化版本,你可以看到完整的代码herethis post上的完整演示。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
  • 2014-06-09
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 2019-02-15
相关资源
最近更新 更多