问题
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)

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)

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.
Any help is appreciated.
EDIT.
Modified the epsg of the multipolygon
in order to match the points
epsg. The issues remains though.
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)
and got this:

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.
回答1:
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)
Which produces:

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.
来源:https://stackoverflow.com/questions/50499991/plotting-points-and-multipolygon-objects-in-r-with-sf