Custom key added to sf plot fails

陌路散爱 提交于 2019-12-11 07:45:41

问题


Trying to resolve this question I came up with this:

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

layout(matrix(1:2, ncol = 2), widths = c(1, lcm(2)))
# 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)
# plot the key
.image_scale_factor(levels(factor(points$p_cut)), col = brewer.pal(5, 'Paired'), 
                    key.length = lcm(8), key.width = lcm(2), key.pos = 4, 
                    at = 1:length(levels(points$p_cut)))

Which should work as it works without the points object:

layout(matrix(1:2, ncol = 2), widths = c(1, lcm(2)))
# sf object with the extent of the points 
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 the 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 key
.image_scale_factor(levels(factor(points$p_cut)), col = brewer.pal(5, 'Paired'), 
                        key.length = lcm(8), key.width = lcm(2), key.pos = 4, 
                        at = 1:length(levels(points$p_cut)))

Even if only the points are plot all fails again:

layout(matrix(1:2, ncol = 2), widths = c(1, lcm(2)))
# plot the points
plot(points['p_cut'], axes = TRUE, graticule = TRUE, pch = 16, key.pos = NULL, pal = brewer.pal(5, 'Paired'))
# plot the key
.image_scale_factor(levels(factor(points$p_cut)), col = brewer.pa(5, 'Paired'), key.length = lcm(8), key.width = lcm(2), key.pos = 4, at = 1:length(levels(points$p_cut)))

Which gives the same output as the first image with this error message:

Error in par(opar[-desel]) : el valor especificado del parámetro del gráfico "pin" es inválido

What is happening? Why when plotting the points object and the key it ends in error?

来源:https://stackoverflow.com/questions/50502536/custom-key-added-to-sf-plot-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!