I'm trying to create a chloropleth map of the US that uses a categorical variable for the state color, but I only get a blank map. Do plotly maps have compatibility with categorical data? If so, how does the syntax change?
For my data, I'm simply uploading a table of rows consisting the state and randomly one of "Good", "Bad", "OK."
What can I change in the code below for it to work? I've tried a workaround that slightly works to change the states' color but the colorbar gets wonky. (value4 is my Categorical Variable of "Good", "Bad", "OK")
Apologies if my question is not clear or my info is not great. I can answer further questions if anyone has them. Thanks in advance
foo <- brewer.pal(n = 3,
name = "Set1")
df <- mutate(df, test = ntile(x = value4, n = 3))
cw_map <- plot_ly(
data = df,
type = "choropleth",
locations = ~ state,
locationmode = "USA-states",
color = ~ test,
colors = foo[df$test],
z = ~ test
) %>%
layout(geo = list(scope = "usa"))
print(cw_map)