R:将非pch符号添加到地图(多边形层)
R: Add non-pch symbols to a map (polygon layer)
我在这里使用 R 作为 GIS,来创建地图。这是一个单色 (black/white) 网站计划。但是,我想使用 these open triangular symbols for coniferous forests 将森林与其他地区区分开来,这在德语世界的制图学中很常见。
一些研究使我找到了 TeachingDemos
包中的 my.symbols
函数。我知道可以编写一个函数,在 xlim = c(-1,1)
、ylim = c(-1,1)
图中绘制所需的符号,然后使用 TeachingDemos::my.symbols
将此符号与 points
函数一样添加到图中。我的计划是给出 TeachingDemos::my.symbols
森林区域内网格的坐标。
我设法编写了一个绘制符号的函数:
nw <- function(){
par(oma = c(0,0,0,0), mar = c(0,0,0,0))
plot(c(-0.84, 0), c(-0.4, 0.67), xlim = c(-1,1), ylim = c(-1,1), type = "l", lwd = 2)
segments(0, 0.67, 0.84, -0.4, lwd = 4)
}
但我没能以正确的方式将函数传递给 my.symbols
。我也没有设法仅在森林多边形中扩展网格,因此将其扩展到多边形图层的整个边界框 (bbox
) 和 select 仅位于森林中的点。类似于:
library(maptools)
nc1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=NAD27"))
cd <- c(100, 50)
grd <- GridTopology(cellcentre.offset = nc1@bbox[, 1],
cellsize = rep(diff(as.numeric(nc1@bbox["x",]))/100, 2),
cells.dim = cd)
grd.sp <- SpatialPixelsDataFrame(grd,
data = data.frame(id = 1:prod(cd)),
proj4string = CRS(proj4string(nc1)))
x11(10, 6)
plot(nc1)
points(coordinates(grd.sp[nc1[which(nc1$AREA > 0.15),],]))
在带分隔符的多边形中绘制特定图像
您可以使用库 sp
对特定多边形中的点进行采样。在我的示例中,我 select 根据 NAME
.
的特定多边形
然后你可以使用库 emojifont
从 emoji 或 fontawesome 添加一些符号。例如,您可以在列表中找到树,例如 evergreen_tree
。或者您可以重复绘制自己的 png 图像。
在分隔的多边形中创建点的规则网格
因此,首先为特定多边形创建网格:
library(maptools)
library(rgdal)
library(sp)
library(emojifont)
nc1 <- rgdal::readOGR(system.file("shapes/sids.shp", package = "maptools")[1])
# Do a point grid in a specific polygon
grid.pt <- sp::spsample(nc1[which(nc1$NAME == "Alamance"),], n = 20, type = "regular")
将表情符号绘制为重复的图像模式
然后使用 grid.pt
将表情符号绘制为文本:
# plot the entire map with restricted limits
plot(nc1, xlim = c(-79.6, -79.1), ylim = c(35.8, 36.3))
# Highlight the selected polygon
plot(nc1[which(nc1$NAME == "Alamance"),], border = "red", lwd = 3, add = TRUE)
# Normal points
points(grid.pt, pch = 20, col = "blue", cex = 0.1)
# Add emojifont instead of points
text(coordinates(grid.pt)[, 1], coordinates(grid.pt)[, 2],
labels = emoji('evergreen_tree'), cex = 1.5,
col = 'forestgreen', family = 'EmojiOne')
将您自己的图像绘制为重复模式
您也可以使用自己的png图片作为点。在此响应中,在网络上下载了一张图像作为可重现的示例:https://gis.stackexchange.com/questions/118286/is-it-possible-to-create-a-scatterplot-with-icons-svg-or-png-instead-of-points
但是,您可以创建和使用自己的 png 图像。
# Plot with your own image
library(png)
# Image downloaded as an example for reproducibility
iconfile1 <- download.file('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Status-weather-clouds-icon.png',
destfile = 'icon1.png', mode = 'wb')
icon1 <- png::readPNG('icon1.png')
# Depending on the desired size of the image
offset <- 0.02
# plot the entire map with restricted limits
plot(nc1, xlim = c(-79.8, -78.9), ylim = c(35.6, 36.5))
# Plot all images at points locations
for (i in 1:length(grid.pt)) {
graphics::rasterImage(
icon1,
coordinates(grid.pt)[i, 1] - offset,
coordinates(grid.pt)[i, 2] - offset,
coordinates(grid.pt)[i, 1] + offset,
coordinates(grid.pt)[i, 2] + offset
)
}
我在这里使用 R 作为 GIS,来创建地图。这是一个单色 (black/white) 网站计划。但是,我想使用 these open triangular symbols for coniferous forests 将森林与其他地区区分开来,这在德语世界的制图学中很常见。
一些研究使我找到了 TeachingDemos
包中的 my.symbols
函数。我知道可以编写一个函数,在 xlim = c(-1,1)
、ylim = c(-1,1)
图中绘制所需的符号,然后使用 TeachingDemos::my.symbols
将此符号与 points
函数一样添加到图中。我的计划是给出 TeachingDemos::my.symbols
森林区域内网格的坐标。
我设法编写了一个绘制符号的函数:
nw <- function(){
par(oma = c(0,0,0,0), mar = c(0,0,0,0))
plot(c(-0.84, 0), c(-0.4, 0.67), xlim = c(-1,1), ylim = c(-1,1), type = "l", lwd = 2)
segments(0, 0.67, 0.84, -0.4, lwd = 4)
}
但我没能以正确的方式将函数传递给 my.symbols
。我也没有设法仅在森林多边形中扩展网格,因此将其扩展到多边形图层的整个边界框 (bbox
) 和 select 仅位于森林中的点。类似于:
library(maptools)
nc1 <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=NAD27"))
cd <- c(100, 50)
grd <- GridTopology(cellcentre.offset = nc1@bbox[, 1],
cellsize = rep(diff(as.numeric(nc1@bbox["x",]))/100, 2),
cells.dim = cd)
grd.sp <- SpatialPixelsDataFrame(grd,
data = data.frame(id = 1:prod(cd)),
proj4string = CRS(proj4string(nc1)))
x11(10, 6)
plot(nc1)
points(coordinates(grd.sp[nc1[which(nc1$AREA > 0.15),],]))
在带分隔符的多边形中绘制特定图像
您可以使用库 sp
对特定多边形中的点进行采样。在我的示例中,我 select 根据 NAME
.
的特定多边形
然后你可以使用库 emojifont
从 emoji 或 fontawesome 添加一些符号。例如,您可以在列表中找到树,例如 evergreen_tree
。或者您可以重复绘制自己的 png 图像。
在分隔的多边形中创建点的规则网格
因此,首先为特定多边形创建网格:
library(maptools)
library(rgdal)
library(sp)
library(emojifont)
nc1 <- rgdal::readOGR(system.file("shapes/sids.shp", package = "maptools")[1])
# Do a point grid in a specific polygon
grid.pt <- sp::spsample(nc1[which(nc1$NAME == "Alamance"),], n = 20, type = "regular")
将表情符号绘制为重复的图像模式
然后使用 grid.pt
将表情符号绘制为文本:
# plot the entire map with restricted limits
plot(nc1, xlim = c(-79.6, -79.1), ylim = c(35.8, 36.3))
# Highlight the selected polygon
plot(nc1[which(nc1$NAME == "Alamance"),], border = "red", lwd = 3, add = TRUE)
# Normal points
points(grid.pt, pch = 20, col = "blue", cex = 0.1)
# Add emojifont instead of points
text(coordinates(grid.pt)[, 1], coordinates(grid.pt)[, 2],
labels = emoji('evergreen_tree'), cex = 1.5,
col = 'forestgreen', family = 'EmojiOne')
将您自己的图像绘制为重复模式
您也可以使用自己的png图片作为点。在此响应中,在网络上下载了一张图像作为可重现的示例:https://gis.stackexchange.com/questions/118286/is-it-possible-to-create-a-scatterplot-with-icons-svg-or-png-instead-of-points
但是,您可以创建和使用自己的 png 图像。
# Plot with your own image
library(png)
# Image downloaded as an example for reproducibility
iconfile1 <- download.file('http://icons.iconarchive.com/icons/oxygen-icons.org/oxygen/256/Status-weather-clouds-icon.png',
destfile = 'icon1.png', mode = 'wb')
icon1 <- png::readPNG('icon1.png')
# Depending on the desired size of the image
offset <- 0.02
# plot the entire map with restricted limits
plot(nc1, xlim = c(-79.8, -78.9), ylim = c(35.6, 36.5))
# Plot all images at points locations
for (i in 1:length(grid.pt)) {
graphics::rasterImage(
icon1,
coordinates(grid.pt)[i, 1] - offset,
coordinates(grid.pt)[i, 2] - offset,
coordinates(grid.pt)[i, 1] + offset,
coordinates(grid.pt)[i, 2] + offset
)
}