rplotlymapscategorical-data

How to change state border color in R Plotly choropleth map?


I have replicated the choropleth map for discrete colors in R using the method suggested in this link: How to create a chloropleth map in R Plotly based on a Categorical variable?

However, as you will notice, the state lines / boundaries in the plot is blue. I'd like this to be black so that the plot is clearer.

I tried the following code below but it doesn't seem to work.

cw_map <- plot_ly(
  data = df,
  type = "choropleth",
  locations = ~ state,
  locationmode = "USA-states",
  z = df$test,
  colorscale=colorScale,
  colorbar=list(tickvals=1:nfactor, ticktext=names(foo))
) %>%
layout(geo = list(scope = "usa", showsubunits = TRUE, subunitcolor = "black")) 

Solution

  • Specify the color you'd like the outline to be as a marker argument to the choropleth construction layer (either the original plot_ly call or the add_trace):

    cw_map <- plot_ly(
      data = df,
      type = "choropleth",
      locations = ~ state,
      locationmode = "USA-states",
      z = df$test,
      colorscale=colorScale,
      colorbar=list(tickvals=1:nfactor, ticktext=names(foo)),
      marker = list(line=list(color = 'black'))
    ) %>%
      layout(geo = list(scope = "usa", showsubunits = TRUE, subunitcolor = "black"))
    

    screenshot of plotly map with black outlines instead of blue