SymbolixAU / mapdeck

R interface to Deck.gl and Mapbox
https://symbolixau.github.io/mapdeck/articles/mapdeck.html
363 stars 40 forks source link

Legend order is incorrect for factors #374

Closed timathomas closed 1 year ago

timathomas commented 1 year ago

Hi everyone! Love the package, usually use leaflet, but wanting to give mapdeck a try for its simplicity and speed for my students.

My results have the legend in the wrong order for a factored categorical value. It seems to be sorting by first character value rather than the factor order I desire. Any help would be wonderful! Thanks!

library(tigris)
library(mapdeck)
library(dplyr)

df <-
  counties(state = "OR") %>%
  bind_cols(
    as.data.frame(matrix(runif(n=36, min=0, max=.25), nrow=36))
    ) %>%
   mutate(rate_cat =
    factor(
      case_when(
          V1 == 0 ~ "0%",
          V1 > 0 & V1 < .021 ~ "0.1% - 2%",
          V1 >= .021 & V1 < .061 ~ "2.1% - 6%",
          V1 >= .061 & V1 < .121 ~ "6.1% - 12%",
          V1 >= .121 ~ paste0("12.1% - ", scales::percent(max(V1, na.rm = TRUE), accuracy = .1)
          )
      ),
    levels = c("0%", "0.1% - 2%", "2.1% - 6%", "6.1% - 12%", paste0("12.1% - ", scales::percent(max(V1, na.rm = TRUE), accuracy = .1))
    )
  )
  )

mapdeck(token = key, style = mapdeck_style("light")) %>%
  add_polygon(
    data = df,
    fill_colour = "rate_cat",
    tooltip = "V1",
    fill_opacity = .5,
    legend = TRUE, 
    palette = "ylorrd"
    )
Screenshot 2023-09-29 at 9 02 19 AM

Also, is there a way other than add_text to put the basemap at the bottom, stack polygons, then add labels on top of the polygons? Like addProviderTiles at different zIndex's?

timathomas commented 1 year ago

I was able to figure out the order using a manual approach. But now having trouble with getting opacity.

my_colors = bind_cols(rate_cat = factor(levels(df$rate_cat)), colors = c("#fecc5c","#fd8d3c","#f03b20","#bd0026","#51087e"))

df2 <- left_join(df, my_colors)

l1 <- legend_element(
  variables = levels(df$rate_cat),
  colours = c("#fecc5c","#fd8d3c","#f03b20","#bd0026","#51087e"),
  colour_type = "fill",
  variable_type = "category",
  title = "Eviction rate"
  )

ml <- mapdeck_legend(l1)

mapdeck(token = key, style = mapdeck_style("light")) %>%
  add_polygon(
    data = df2,
    fill_colour = "colors",
    # fill_colour = "rate_cat",
    tooltip = "V1",
    fill_opacity = .5,
    legend = ml
    )
timathomas commented 1 year ago

The opacity issue relates to #292 where I needed to add the opacity to the hex value. Final code and closing this issue.

library(tigris)
library(mapdeck)
library(dplyr)

df <-
  counties(state = "OR") %>%
  bind_cols(
    as.data.frame(matrix(runif(n=36, min=0, max=.25), nrow=36))
    ) %>%
   mutate(rate_cat =
    factor(
      case_when(
          V1 == 0 ~ "0%",
          V1 > 0 & V1 < .021 ~ "0.1% - 2%",
          V1 >= .021 & V1 < .061 ~ "2.1% - 6%",
          V1 >= .061 & V1 < .121 ~ "6.1% - 12%",
          V1 >= .121 ~ paste0("12.1% - ", scales::percent(max(V1, na.rm = TRUE), accuracy = .1)
          )
      ),
    levels = c("0%", "0.1% - 2%", "2.1% - 6%", "6.1% - 12%", paste0("12.1% - ", scales::percent(max(V1, na.rm = TRUE), accuracy = .1))
    )
   )
  )

my_colors <-
  bind_cols(
    rate_cat = factor(levels(df$rate_cat)),
    colors = c("#fecc5c50","#fd8d3c50","#f03b2050","#bd002650","#51087e50"),
    opacity = rep(.5, 5)
    )

df2 <- left_join(df, my_colors)

l1 <- legend_element(
  variables = levels(df$rate_cat),
  colours = c("#fecc5c50","#fd8d3c50","#f03b2050","#bd002650","#51087e50"),
  colour_type = "fill",
  variable_type = "category",
  title = "Eviction rate"
  )

ml <- mapdeck_legend(l1)

mapdeck(token = key, style = mapdeck_style("light")) %>%
  add_polygon(
    data = df2,
    fill_colour = "colors",
    tooltip = "V1",
    legend = ml
    )