I'm working on an R shiny app and I have a dataframe with States and Population data that I'm trying to plot with plotly. This is my code:
# create a vector of state abbreviations
abbreviations <- c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY")
# create a vector of state populations
populations <- c(4903185, 731545, 7278717, 3017825, 39538223, 5758736, 3565287, 973764, 21477737, 10617423, 1415872, 1787065, 12671821, 6732219, 3155070, 2913314, 4467673, 4648794, 1344212, 6045680, 6892503, 9986857, 5639632, 2976149, 6137428, 1068778, 1934408, 3080156, 1359711, 8882190, 2096829, 19453561, 10488084, 762062, 11689100, 3956971, 4217737, 12801989, 1059361, 5148714, 884659, 6829174, 28995881, 3205958, 623989, 8535519, 7693612, 1792147, 5822434, 1793716, 578759)
# create the dataframe
us_states <- data.frame(state = abbreviations, population = populations)
aa_plotly__create_US_states_map <- function(df, states_col='',data_col='',
plot_title='U.S. States map', colors_var = 'YlGn', colors_bar_title='', colors_bar_tickprefix = '', show_lakes=T,
boundareies_line_width = 1.0, display_states_labels=F, states_labels_text_size=12,
verbose=F, verbose_debug=F){
# resources:
# https://plotly.com/r/builtin-colorscales/
# https://plotly.com/r/choropleth-maps/ it shows also example to plot counties
# https://search.r-project.org/CRAN/refmans/plotly/html/plot_geo.html example of longitude and altitude
if(colors_bar_title==''){
colors_bar_title = data_col
}
df$hover <- paste0('State: ',df[,states_col], '<br>', data_col, df[, data_col])
# light grey boundaries
l <- list(color = toRGB("black"), width = boundareies_line_width)
p <- plot_geo(df, locationmode = "USA-states") %>%
add_trace(z = df[, data_col]
, locations = df[,states_col]
#, text = df[,states_col]
, color = df[, data_col]
, colors = colors_var # working values "Purples", "RdBu" https://plotly.com/r/builtin-colorscales/
, showscale = T
, text = df$hover
, marker = list(line = l)
) %>%
colorbar(title = colors_bar_title
, tickprefix = colors_bar_tickprefix # colors_bar_tickprefix can get values like '$'
) %>%
layout(title = plot_title,
geo = list(scope = "usa" #if i omit this, i get the entire world!, another accepted value is 'europe'
#,projection = list(type = 'albers usa')
, showlakes = show_lakes
#,lakecolor = toRGB('white')
)
)
if(display_states_labels){
# https://stackoverflow.com/questions/68181278/adding-labels-to-plotly-map-created-using-plot-geo
p <- p %>% add_trace(type = "scattergeo", locationmode = 'USA-states',
locations = df[,states_col],
text = df[,states_col],
mode = "text",
textfont = list(color = rgb(0,0,0),
size = states_labels_text_size)
)
}
if(verbose) cat("\n\n========== aa_plotly__create_plot_with_subplots_that_all_have_same_xaxis ==================== \n\n")
return(p)
}
aa_plotly__create_US_states_map(df, states_col='state',data_col='population',
plot_title='U.S. States map', colors_var = 'YlGn', colors_bar_title='', colors_bar_tickprefix = '', show_lakes=T,boundareies_line_width = 1,display_states_labels=T, states_labels_text_size=12,
verbose=F, verbose_debug=F)
Everything works well besides the labels part. I enhanced the function to add states labels using answer found here:Adding labels to plotly map created using plot_geo That part of code that add labels fails and does not add any label to the plot. What's wrong in the code? Thanks