Search code examples
rplotleafletr-mapview

plotting point data in Mapview - non-linear scale for point symbol sizes?


Is there a way with the mapview library to plot the size of point symbols using a non-linear scale? For example, with geometric intervals, a custom vector of intervals, or intervals based on something like the Jenks' natural breaks method? (mapview's 'setting point size 'cex' linearly': https://r-spatial.github.io/mapview/articles/articles/mapview_02-advanced.html )

My use case is plotting many points of river gage flow data, but enormously high and low values at the coast from ocean tides dominate the scale, to a point that essentially all of the non-coastal locations appear to have symbol sizes (representing river flow) that are barely discernibly different, whereas I'm hoping the user can easily visualize differences like creeks vs rivers (eg here, three groups with values in the 1000s, another in the 5000s, and another in the 9000s).

library(mapview)
library(sf)

lat <- seq(1, 20, 1)
lon <- lat
value_to_visualize <- c(461000, 1500, 1400, 1450, 1350, 1100, 1400, 2000, 9040, 9060, 
                    9080, 9990, 9995, 5750, 5500, 5400, 5300, 5100, 5050, -60000)

df <- data.frame(lat,lon, value_to_visualize)
df <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326) 

m <- mapview(df["value_to_visualize"], 
         cex = "value_to_visualize",
         legend = TRUE,
         layer.name = "cfs")  
m

enter image description here


Solution

  • library(mapview)
    library(sf)
    
    lat <- seq(1, 20, 1)
    lon <- lat
    value_to_visualize <- c(461000, 1500, 1400, 1450, 1350, 1100, 1400, 2000, 9040, 9060, 
                            9080, 9990, 9995, 5750, 5500, 5400, 5300, 5100, 5050, -60000)
    
    
    cluster_to_visualize <- dplyr::case_when(value_to_visualize<1000 ~ 1,
                                             value_to_visualize<5000 ~ 3,
                                             value_to_visualize<9000 ~ 5,
                                             value_to_visualize>9000 ~ 7)
    
    df <- data.frame(lat,lon, cluster_to_visualize, value_to_visualize)
    df <- st_as_sf(df, coords = c("lon", "lat"), crs = 4326) 
    
    m <- mapview(df["value_to_visualize"], 
                 cex = df$cluster_to_visualize,
                 legend = TRUE,
                 layer.name = "cfs") 
    

    Created on 2019-06-05 by the reprex package (v0.3.0)