1

Is it possible to use a non viridisLite palette with googleway? Specifically, I want to have a single colour scale. Here is my attempt:

library(googleway)
library(RColorBrewer)
googleway::google_map("") %>% 
  googleway::add_polygons(data = melbourne, 
               polyline = "polyline", 
               fill_colour = "SA4_NAME", 
               palette = brewer.pal(9, "Purples")
               )

Result:

Error in checkPalettes.default(arg) : 
  I don't recognise the type of palette you've supplied
Vlad
  • 3,058
  • 4
  • 25
  • 53

1 Answers1

1

The palette argument is expecting a function which generates a palette given a single argument as input (not a palette of colours itself)

You can wrap the brewer.pal() function in your own function with a single input.

However, the trouble you're going to face is mapping the 9 colours from brewer.pal to the 12 SA4_NAME variables. 9 into 12 doesn't go.

This is how it would work if you had a variable with 9 different values.

library(googleway)
library(RColorBrewer)

set_key(read.dcf("~/Documents/.googleAPI", fields = "GOOGLE_MAP_KEY"))

myBrewerPal <- function(x) { brewer.pal(n = x, name = "Purples") }
melbourne$myVal <- sample(1:9, size = nrow(melbourne), replace = T)

googleway::google_map("") %>% 
  googleway::add_polygons(
    data = melbourne
    , polyline = "polyline"
    , fill_colour = "myVal"
    , palette = myBrewerPal
    )

enter image description here


An alternative solution is to manually add a column of hex colours to melbourne, and specify that column as the fill_colour. Then it's up to you to make sure the colours are mapped correctly to the variables you want.

melbourne$myColours <- sample(brewer.pal(n = 9, name = "Purples"), size = nrow(melbourne), replace = T)

googleway::google_map("") %>% 
  googleway::add_polygons(
    data = melbourne
    , polyline = "polyline"
    , fill_colour = "myColours"
  )
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • Since `brewer.pal` only does 9 colours, I've created a rank, rescaled and rounded to 1:9 and am passing that to the palette function. It gets around dealing with abnormally large values that make the other polygons look almost the same colour. ` ... %>% mutate(rank = round(rescale(dense_rank(myVariable), to=c(1,9))))` – Vlad Sep 05 '18 at 02:14