我有一张波斯尼亚地图,其中的市镇根据居住在那里的少数民族而着色。
但是,我想使用不同的图案而不是颜色(或灰度),因为它将以黑白打印。
我搜索过,但找不到办法。有没有人知道如何做到这一点?
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
这给了我以下地图:
答案 0 :(得分:12)
为基本图形抛弃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
用相同的参数做一个很好的图例。
答案 1 :(得分:1)
有点晚了,但这可能会有所帮助。您可以使用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)