Legend order is incorrect for factors
timathomas opened this issue · comments
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](https://private-user-images.githubusercontent.com/30070711/271658326-98670c82-7178-48e9-98a4-f0e90491408b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQwNDM1NTAsIm5iZiI6MTcyNDA0MzI1MCwicGF0aCI6Ii8zMDA3MDcxMS8yNzE2NTgzMjYtOTg2NzBjODItNzE3OC00OGU5LTk4YTQtZjBlOTA0OTE0MDhiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTklMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODE5VDA0NTQxMFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTY3MGM3MGI1NmY0M2JiOWUxMWY0NWMwNTI4NTEyNGJjMTg3MDc0NTA5YmQyNTkyMjZkZGZmZmQ3NjdmZTlmN2MmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.9TuFMI_qUGq3QxfGijnSXc4yzKA-6B51x7upqjA8yRg)
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?
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
)
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
)