用图案填充地理空间多边形 - R [英] Fill Geospatial polygons with pattern - R

查看:102
本文介绍了用图案填充地理空间多边形 - R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据居住在那里的种族多数,我有一张波斯尼亚的城市地图。
但是,我想使用不同的图案而不是颜色(或灰度),因为它将以黑白色打印。

我搜索了,但找不到方法来做到这一点。有没有人有关于如何做到这一点的想法?
链接到shapefile



以下是我的代码:

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

setwd(路径)

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

塞尔维亚人< - 波斯尼亚人[bosnia $ SEPRIORITY> bosnia $ CRPRIORITY&波黑$ SEPRIORITY> bosnia $ MOPRIORITY,]
croats< - 波斯尼亚[bosnia $ CRPRIORITY>波斯尼亚$ SEPRIORITY& bosnia $ CRPRIORITY> bosnia $ MOPRIORITY,]
moslems< - 波斯尼亚[bosnia $ MOPRIORITY> bosnia $ CRPRIORITY& bosnia $ MOPRIORITY> (波斯尼亚,aes(x = long,y = lat,group =组))+
geom_polygon(aes(x = long,y = lat = group = group),fill =white,color =gray)+
geom_polygon(data = serbs,aes(x = long,y = lat,group = group),fill =black ,color =gray)+
geom_polygon(data = croats,aes(x = long,y = lat,group = group),fill =green,color =gray)+
geom_polygon(data = moslems,aes(x = long,y = lat,group = group),fill =red,color =gray)+
#样式
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

这给了我以下地图:

解决方

ggplot 为基础的图形。虽然只有三个团体,我会认为黑色,白色和中等灰色可以正常工作。

  require(sp)
require(rgdal)

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

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

  serbs = bosnia $ SEPRIORITY> bosnia $ CRPRIORITY&波黑$ SEPRIORITY> bosnia $ MOPRIORITY 
croats = bosnia $ CRPRIORITY>波斯尼亚$ SEPRIORITY& bosnia $ CRPRIORITY>波斯尼亚$ MOPRIORITY
moslems =波斯尼亚$ MOPRIORITY> bosnia $ CRPRIORITY& bosnia $ MOPRIORITY>波斯尼亚$ SEPRIORITY

bosnia $ group = NA
bosnia $ group [serbs] =塞尔维亚人
波斯尼亚$ group [croats] =克罗地亚人
波斯尼亚$ group [moslems] =Moslem
bosnia $ group =因子(bosnia $ group)

检查没有人属于多个类别:

  sum(serbs&&&&&&moslems)#应为零

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

  spplot(bosnia,group)

但我不明白如何在不同的单声道风格中做到这一点,所以它回到基础图形:

  plot(波斯尼亚,密度= c(5,10,15)[bosnia $ group],angle = c(0,45,90)[bosnia $ group])



根据喜好调整参数。您可以使用 legend 来制作具有相同参数的精美图例。


I have a map of Bosnia with municipalities colored according to the ethnic majority living there. However, I would like to use different patterns instead of colors (or grey scales), as it's going to be printed in black and white.
I have searched, but couldn't find a way to do it. Does anyone have an idea on how to do this? Link to shapefile

Here's my code so far:

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

This gives me the following map:

解决方案

Ditch ggplot for base graphics. Although with only three groups I would have thought black, white and mid-grey would work okay.

require(sp)
require(rgdal)

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

Instead of splitting into 3 data sets, make a single new categorical variable from three 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)

Check nobody is in more than one category:

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

Now you can get a pretty coloured plot thus:

spplot(bosnia, "group")

But I can't see how to do that in different mono styles, so its back to base graphics:

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

Adjust parameters to taste. You can use legend to do a nice legend with the same parameters.

这篇关于用图案填充地理空间多边形 - R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆