基于地图视图中不同变量的点颜色和符号大小

Point color and symbol size based on different variables in mapview

我正在尝试在 mapview 中使用不同比例的主题来帮助可视化收益与损失,其中:

有什么想法吗?


library(tidyverse)
library(mapview)
library(sf)

lat <- rep(34,16)
lon <- seq(-128, -126, length = 16)
value <- c(-1000, -800, -600, -400, -200, -100, -50, 
            -25, 25, 50, 100, 200, 400, 600, 800, 1000)

#make data.frame
df <- data.frame(lat, lon, value) 

#make spatial object for mapview
df <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326) %>%
      mutate(value_abs = abs(value)) #value_abs intended for `cex` argument

pal <-  mapviewPalette("mapviewSpectralColors") #from mapview doc. example
m   <-  mapview(df["value"], #sets hover over value as this column
         cex = "value",      #sets circle diameter scaling on this column
         legend = TRUE,
         col.regions = pal(100), #closest I found to a red-blue divergent scale
         layer.name = "value")  
m

换句话说,我希望下面的点图案与左侧对称,大小为右侧的镜像,但左边是蓝色圆圈,右边是红色圆圈,仍然允许用户通过鼠标悬停查看实际(非绝对)值(例如 -1000)。

次尝试:将 cex = "value" 切换为 cex = "value_abs" 会产生 warning: In min(x) : no non-missing arguments to min; returning Inf 且不绘制任何点,或者使用 cex = df$value_abs(无引号),这会产生无色的巨大点。我不打算需要两个图例 - 只需要一个用于圆圈大小或填充,像现在一样显示最小值和最大值,那就太好了。

你们很亲近。您需要明确引用 df$value_abs。往下看:

library(tidyverse)
library(mapview)
library(sf)

df <- data.frame(lat=rep(34,16), 
                 lon=seq(-128, -126, length = 16), 
                 value=c(-1000, -800, -600, -400, -200, -100, -50, 
                         -25, 25, 50, 100, 200, 400, 600, 800, 1000)) 

df <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326) %>%
               mutate(value_abs = abs(value))

pal <-  mapviewPalette("mapviewSpectralColors")

mapview(df["value"], 
                cex = df$value_abs/100, 
                legend = TRUE,
                col.regions = pal(100), 
                layer.name = "value")  

reprex package (v0.3.0)

于 2019-06-24 创建