I'm creating a chloropleth map in R using plotly, and the only trouble I'm having is setting a different colorscale. I would like to use the magma colorscale from the viridis package, but I can't seem to figure out the correct way to do it. I've tried googling and searching, but no answers are quite working. Anyone have any advice?
The error I'm getting is: "unique() only applies to vectors." I've tried setting "discrete = TRUE" but that does not work.
Let me know if you need more information.
create_cw_map <- function(data, color_var) {
if (is.null(data))
return(NULL)
g <- list(scope = "usa",
projection = list(type = "albers usa"),
showlakes = FALSE)
cw_map <- plot_geo(data,
locationmode = "USA-states") %>%
add_trace(z = ~ get(color_var),
locations = ~ state,
color = ~ get(color_var),
colorscale = scale_fill_viridis(option = "magma")) %>%
colorbar(title = color_var) %>%
layout(geo = g)
print(cw_map)
}