问题描述
我一直在尝试绘制 points
和 multipolygon
sf
对象,但没有成功.以下是我的问题的可重现示例.
I've been trying to plot both points
and multipolygon
sf
objects with no success. Following is a reproducible example of my problem.
library(sf)
library(magrittr)
library(RColorBrewer)
nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>%
st_transform(crs = 4326)
points <- data.frame(p = seq(15, 75, 15),
long = c(-85, -80, -78, -75, -82),
lat = c(34, 36, 37, 38, 35)) %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326)
points$p_cut <- cut(points$p, seq(0, 100, 20))
#plot1
plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
pal = brewer.pal(5, 'Paired'))
plot(st_geometry(nc), axes = TRUE, graticule = TRUE, add = TRUE)
multipolygon
不会出现,但如果我颠倒顺序:
The multipolygon
doesn't appear but if I inverse the order:
#plot2
plot(st_geometry(nc), axes = TRUE, graticule = TRUE)
plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
pal = brewer.pal(5, 'Paired'), add = TRUE)
最终情节中缺少两点和关键.
Two points and the key are missing from the final plot.
我该如何克服这些问题?我想改变 multipolygon
对象的 bbox
以使缺失的点出现在第二个图中,但我找不到办法.
How can I overcome these issues? I thought of changing the bbox
of the multipolygon
object in order to make the missing points appear in the second plot but I couldn't find a way for this.
感谢任何帮助.
编辑.
修改了 multipolygon
的 epsg 以匹配 points
epsg.问题仍然存在.
Modified the epsg of the multipolygon
in order to match the points
epsg. The issues remains though.
EDIT2.这很奇怪,但随机运行代码似乎会产生正确的情节".我使用了这个特定的顺序(从控制台复制):
EDIT2.It's weird but running the code randomly seems to produce the "correct plot". I used this particular order (copied from the console):
> plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
+ pal = brewer.pal(5, 'Paired'))
> plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
+ pal = brewer.pal(5, 'Paired'), add = TRUE)
> plot(st_geometry(nc), axes = TRUE, graticule = TRUE, add = TRUE)
> #plot2
> plot(st_geometry(nc), axes = TRUE, graticule = TRUE)
> plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
+ pal = brewer.pal(5, 'Paired'), add = TRUE)
> # plot1
> plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
+ pal = brewer.pal(5, 'Paired'))
> plot(st_geometry(nc), axes = TRUE, graticule = TRUE, add = TRUE)
> plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16,
+ pal = brewer.pal(5, 'Paired'), add = TRUE)
得到这个:
缺失的点之一出现 (-85,34) 但有重复点.此外,密钥未正确显示.重复点的位置和多边形的大小似乎也取决于绘图查看器的大小.
One of the missing points appears (-85,34) but there are repeated points. Also the key is not shown properly. It seems too that the location of the repeated points and the size of the multipolygon depend on the size of the plot viewer.
推荐答案
我使用了一个空白图和更大的图的范围(在这种情况下,更大的范围属于 points
对象)和向其中添加了 points
和 multipolygon
对象:
I used a blank plot with the extent of the bigger plot (in this case the bigger extent belongs to the points
object) and added both the points
and the multipolygon
objects to it:
# sf object with the extent of the points object
bb_sol <- data.frame(long = c(-85, -75, -75, -85),
lat = c(34, 34, 38, 38)) %>%
st_as_sf(coords = c('long', 'lat'), crs = 4326)
# plot extent
plot(st_geometry(bb_sol), axes = TRUE, graticule = TRUE, pch = '.')
# plot the multipolygon
plot(st_geometry(nc), axes = TRUE, graticule = TRUE, add = TRUE)
# plot the points
plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16, key.pos = NULL,
pal = brewer.pal(5, 'Paired'), add = TRUE)
产生:
缺失的点出现但没有键.为了添加一个密钥,我尝试使用 .image_scale_factor()
函数,但使用 没有好的结果.
The missing points appear but there is no key. In order to add a key I tried to use the .image_scale_factor()
function but with no good results.
这篇关于使用 sf 在 R 中绘制点和多边形对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!