2

I'm currently making a choropleth map in plotly that tracks the population of ever US state from 1910 - 2020. The data is sorted into categorical variables with the percent change of every state in a decade. When I run my code the first year (1910) looks right, however when pressing "play" or moving the slider to any other year the colorscale resets to the default and the discrete scale and ticks are replaced with a continuous version. To be honest, I'm completely lost as to why only the first frame is correct, and when I return to the first frame after navigating to any of the other frames it changes to be like the other frames (colorscale wise).

Here is my code: `

library(plotly)
library(dplyr)
library(readr)
library("RColorBrewer")

states <- read_csv("states.csv")

AllCountryPop <- read_csv("All50%d.csv", 
                         col_types = cols(Year = col_number(), 
                                          Percent = col_number()))%>%
  inner_join(states, by.x = State, by.x = state) %>%
  select(Year, Code, Percent, Category) %>%
  mutate(hover = paste(Code, "\n", 100*Percent, "%"))
  
AllCountryPop$Category = factor(AllCountryPop$Category)
AllCountryPop$Val = as.numeric(AllCountryPop$Category)
nfactor = length(levels(AllCountryPop$Category))
colr <- brewer.pal(n = nfactor,name = "Blues") #Color scale should be blue with five leves (for each category)
levels(AllCountryPop$Category) <- c("-30% - 0%" , "0% - 30%", "30% - 60%", "60% - 90%", "90% - 130%")
names(colr) = levels(AllCountryPop$Category)
colrS = function(n){
  CUTS = seq(0,1,length.out=n+1)
  rep(CUTS,ifelse(CUTS %in% 0:1,1,2))
}

graph_properties <- list(
  scope = 'usa',
  showland = TRUE,
  landcolor = toRGB("white"),
  color = toRGB("white")
)

font = list(
  family = "DM Sans",
  size = 15,
  color = "black"
)

label = list(
  bgcolor = "#EEEEEE",
  bordercolor = "transparent",
  font = font
)

colorScale <- data.frame(z=colrS(nfactor),
        col=rep(colr,each=2),stringsAsFactors=FALSE)
  

  
p <- plot_geo(AllCountryPop, 
              locationmode = "USA-states", 
              frame = ~Year)%>%
      add_trace(locations = ~ Code, 
             locationmode = "USA-states", 
             z = AllCountryPop$Val,
             zmin = min(AllCountryPop$Val),
             zmax = max(AllCountryPop$Val),
             text = ~hover,
             hoverinfo = 'text',
             colorbar=list(tickvals=1:nfactor, ticktext=names(colr)),
            colorscale= colorScale) %>%
      layout(geo = graph_properties,
         title = "Population Percent Change in the US\n1910 - 2020",
         font = list(family = "DM Sans")) %>%
      config(displayModeBar = FALSE) %>%
      style(hoverlabel = label) %>%
            colorbar(title = "Percent")

p

`

In case my CSV data may be of use:

The first two lines of "states.csv": "State","Abbrev","Code" "Alabama","Ala.","AL"

The first two lines of "All50%d.csv": State,Percent,Year,Category Alabama,0.051,2020,0% - 30%

P.S. First time asking a question here, please let me know if I need to change how I ask a question, provide info, etc,

0 Answers0