Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point color and symbol size based on different variables in mapview

I'm trying for a divergent scale theme in mapview to help visualize gains vs. losses, with:

  • point symbol circle sizes on an absolute value scale (to highlight losses as much as gains)
  • a divergent color scale fill for the circles (say dark blue>blue>white>red>dark red for most negative>negative>zero>positive>largest)
  • mouse over hover label of the original value retained

any ideas?


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

In other words, I'm hoping for the pattern of points here below to be symmetrical with the left side as a mirror image of the right in size, but with blue circles at left, red at right, and still allowing the user to see the actual (non absolute) values (e.g. -1000) by mouseover.

enter image description here

attempts: switching cex = "value" with cex = "value_abs" yields warning: In min(x) : no non-missing arguments to min; returning Inf without any points drawn, or with cex = df$value_abs (no quotes), which makes uncolored, enormous points. I'm not planning on needing two legends - just one for either the circle size or fill, showing a min and max value like it does now, would be great.

like image 334
dbo Avatar asked Jun 24 '19 20:06

dbo


People also ask

How to change the size of point data based on color?

Say you categorized your point data based on color using a certain attribute. Simply right click each category and select Change Size. It should display a size dialog box. Then click on the expression filter and then select Size Assistant.

How to add colors to a map view?

You have to add arguments for colors, see ?mapview. In your case for main color you can use col.regions=list ("red","blue") and col=list ("red","blue") for outlines. So whole code could be like this:

How do I change the size of a color symbology?

In the Layer Properties dialog box, click the Symbology tab. On the left side (the Show: box), click Multiple Attributes. In the Value Fields section, select the field containing the attribute to base the color symbology on. Leave the other two fields blank. Click the Add All Values button at the bottom and uncheck . Click the Symbol Size button.

How do I change the size of a layer symbol?

When a layer is symbolized with single symbol, unique values, graduated colors, or unclassed colors, you can also vary the size of the symbols. You can set the size variable to an attribute field, write an expression, or even set the size to random values within a range.


1 Answers

You are very close. You need to explicitly refer to df$value_abs. Look below:

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

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

like image 180
M-- Avatar answered Sep 29 '22 16:09

M--