Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting choropleth maps in a loop not working as expected #900

Closed
NathanDotTo opened this issue Mar 13, 2017 · 1 comment
Closed

Plotting choropleth maps in a loop not working as expected #900

NathanDotTo opened this issue Mar 13, 2017 · 1 comment

Comments

@NathanDotTo
Copy link

NathanDotTo commented Mar 13, 2017

sample_data.zip

This code creates plots that should show the consumption for all states for a given consumable, identified by a MSN code, for a given year.

There is a bug with, possibly, plotting multiple maps that this code illustrates.
It may be related to: #273

The sample data is in the sample_data.zip file attached, which contains four CSV files that are loaded below.

The first example, with the first_map_plot_gatherer, draws four maps, and that works as expected.

The second example, with the second_map_plot_gatherer, puts the four dataframes in a matrix, then iterates over the matrix to create four maps. The code seems to be using a distinct dataframe in each loop iteration, as shown by the print statements, and the title in the map.

BUT, the text value shown when hovering over a state in each map is the same value, reflecting the value in the last dataframe, i.e. the AVTXP_df.

This is probably me misusing the API. Just in case it is not, I am reporting this here.

require("plotly")

ABICB_df <- read.csv("ABICB_df.csv", check.names = FALSE)
CLTXB_df <- read.csv("CLTXB_df.csv", check.names = FALSE)
CLEIB_df <- read.csv("CLEIB_df.csv", check.names = FALSE)
AVTXP_df <- read.csv("AVTXP_df.csv", check.names = FALSE)

#This works

# Make state borders red
borders <- list(color = toRGB("red"))
# Set up some mapping options
map_options <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)
first_map_plot_gatherer <- htmltools::tagList()

first_map_plot_gatherer[[1]] <- plot_ly(z = ~ABICB_df$value, locations = ~ABICB_df$State, type = 'choropleth', locationmode = 'USA-states', color = ABICB_df$value, colors = 'Blues', marker = list(line = borders)) %>%
    layout(title = paste("Debug sum value is -", sum(ABICB_df$value)), geo = map_options)

first_map_plot_gatherer[[2]] <- plot_ly(z = ~CLTXB_df$value, locations = ~CLTXB_df$State, type = 'choropleth', locationmode = 'USA-states', color = CLTXB_df$value, colors = 'Blues', marker = list(line = borders)) %>%
    layout(title = paste("Debug sum value is -", sum(CLTXB_df$value)), geo = map_options)

first_map_plot_gatherer[[3]] <- plot_ly(z = ~CLEIB_df$value, locations = ~CLEIB_df$State, type = 'choropleth', locationmode = 'USA-states', color = CLEIB_df$value, colors = 'Blues', marker = list(line = borders)) %>%
    layout(title = paste("Debug sum value is -", sum(CLEIB_df$value)), geo = map_options)

first_map_plot_gatherer[[4]] <- plot_ly(z = ~AVTXP_df$value, locations = ~AVTXP_df$State, type = 'choropleth', locationmode = 'USA-states', color = AVTXP_df$value, colors = 'Blues', marker = list(line = borders)) %>%
    layout(title = paste("Debug sum value is -", sum(AVTXP_df$value)), geo = map_options)

first_map_plot_gatherer

#This does not work
#The sum in the map title shows that the dataframe is distinct for each map,
#But the data shown for the text value is the same for every map.
msn_subsets_mtrx <- matrix(list(), nrow = 1, ncol = 4)
msn_subsets_mtrx[[1,1]] <- ABICB_df
msn_subsets_mtrx[[1,2]] <- CLTXB_df
msn_subsets_mtrx[[1,3]] <- CLEIB_df
msn_subsets_mtrx[[1,4]] <- AVTXP_df

second_map_plot_gatherer <- htmltools::tagList()
for (col_counter in 1:4) {
  print(col_counter)
  plot_df <- msn_subsets_mtrx[[1, col_counter]]
  print(sum(plot_df$value))
  second_map_plot_gatherer[[col_counter]] <- plot_ly(z = ~plot_df$value, locations = ~plot_df$State, type = 'choropleth', locationmode = 'USA-states', color = plot_df$value, colors = 'Blues', marker = list(line = borders)) %>%
    layout(title = paste("Debug sum value is -", sum(plot_df$value)), geo = map_options)
}
length(second_map_plot_gatherer)
second_map_plot_gatherer
@cpsievert
Copy link
Collaborator

htmltools::browsable(second_map_plot_gatherer) should print the plots for you..

BTW, crosstalk::bscols takes a more modern approach approach on the alignment problem. I recommend checking it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants