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

[ENH] generate Plotly choropleth map of US states & clusters #16

Merged
merged 28 commits into from
May 23, 2024

Conversation

Remi-Gau
Copy link
Contributor

@Remi-Gau Remi-Gau commented May 5, 2024

@Remi-Gau Remi-Gau marked this pull request as draft May 5, 2024 07:46
@michellewang
Copy link
Contributor

michellewang commented May 9, 2024

I have made some changes to @koudyk's code, in the new (and badly named) make_map2() function. It creates a map that looks very similar to what the previous code created, except the main underlying choropleth map's "locations" are the same as the survey's states/state clusters (non-abbreviated names). The old code is still all there, though I am only using a small part of it. We can clean it up later once the map is finalized.

Screenshot 2024-05-09 at 7 27 03 PM

The figure has a few traces/layers:

  1. main_map: choropleth showing all the states/clusters coloured according to the question/subquestion/context. This is probably the one to which any callbacks should be attached
  2. clicked_state: choropleth subset with only a single state/cluster, coloured the same as above and with (customizable) border highlights
  3. hover_info: another choropleth map, with all US states but using the Plotly default US map. This trace is the only one with hover boxes activated (the polygons here are fully transparent). The reason why I did the hover boxes here instead of in main_map is because for some reason the hover boxes in main_map are placed in weird locations (not centered). I'm not sure why, might be something weird with the GeoJSON. Also this way each state within a cluster can have its own hover box (with identical information)
  4. abbr_labels: a "scattergeo" map that adds labels to the states (abbreviated state names). Basically the same as what was there before.

Haven't implemented the impact things yet, but I see in the meeting minutes that we said to test these two approaches:

  1. Change the gradient of the entire map (replacing the existing question gradient)
  2. Add a dot layer with dot color or size corresponding to proportion experienced

I can try both and show screenshots of what that looks like sometime next week, but if someone else would like to try, then please go for it! I need to work on #5 now 😅

@michellewang michellewang changed the title [ENH] generate Plotly cloropleth map of US states & clusters [ENH] generate Plotly choropleth map of US states & clusters May 9, 2024
@michellewang
Copy link
Contributor

For this one I think make_map.py has to be in climate_emotions_map for now because of the import from data_loader... But if we want it in code I can load the files manually without using the data constants

@michellewang
Copy link
Contributor

@alyssadai added visualization of impact data. Now there are three possible views.

Opinion data only:

fig = make_map(
    question="q2",
    sub_question=1,
    outcome="3+",
    clicked_state="Colorado, New Mexico (Cluster E)",
)

Screenshot 2024-05-23 at 1 47 40 AM

Impact data only:

fig = make_map(
    question="q2",
    sub_question=1,
    outcome="3+",
    clicked_state="Colorado, New Mexico (Cluster E)",
    impact="wildfire",
    show_impact_as_gradient=True,
)

Screenshot 2024-05-23 at 1 48 17 AM

Opinion + impact data:

fig = make_map(
    question="q2",
    sub_question=1,
    outcome="3+",
    clicked_state="Colorado, New Mexico (Cluster E)",
    impact="wildfire",
    show_impact_as_gradient=False,
)

Screenshot 2024-05-23 at 1 49 32 AM

More notes:

  • clicked_state is optional
  • impact is optional, but question/sub_question/outcome are all mandatory (for now). Can change if we want it to be optional (e.g. in the case where impact is given and show_impact_as_gradient is True)

@michellewang michellewang marked this pull request as ready for review May 23, 2024 12:34
@alyssadai alyssadai requested a review from surchs May 23, 2024 17:55
Copy link
Contributor

@surchs surchs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @michellewang, very cool PR. I left a couple of comments - mainly for my own understanding. Take a look and see where you agree.

One change I would suggest is to not have a separate colormap for the impact markers (only when impact + opinions are shown at the same time). We already encode impact by the size of the markers - and the impact + opinion color maps clash a bit as well.

Comment on lines +36 to +38
return json.loads(
(Path(__file__).parents[1] / "code" / "assets" / file).read_text(),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍒 do you need the read_text if you would use json.load instead of json.loads?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but you would have to explicitly open the file first and I thought that was a little simpler since we had pathlib.Paths anyway

climate_emotions_map/data_loader.py Outdated Show resolved Hide resolved
Comment on lines 260 to 277
if __name__ == "__main__":

# pick a random state/cluster to highlight
states = state_abbreviations["state"].tolist()
clicked_state = np.random.choice(states)
# clicked_state = "Colorado, New Mexico (Cluster E)" # example cluster
print(f"clicked_state: {clicked_state}")

fig = make_map(
question="q2",
sub_question=1,
outcome="3+",
clicked_state=clicked_state,
impact="wildfire",
show_impact_as_gradient=False,
)

fig.show()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is here mainly for testing, yes?

climate_emotions_map/make_map.py Outdated Show resolved Hide resolved
climate_emotions_map/make_map.py Show resolved Hide resolved
climate_emotions_map/make_map.py Outdated Show resolved Hide resolved
climate_emotions_map/make_map.py Outdated Show resolved Hide resolved
climate_emotions_map/make_map.py Show resolved Hide resolved
Comment on lines +204 to +210
hovertemplate=(
"<b>%{customdata[0]}</b>"
"<br>Sample size: %{customdata[1]}"
f"<br>{col_gradient.capitalize()}: %{{z:.2f}}"
f"{hovertemplate_extra}"
"<extra></extra>"
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neato

Comment on lines 226 to 230
"color": px.colors.sample_colorscale(
impact_colormap,
(row[col_color_impact] - vmin_impact)
/ (vmax_impact - vmin_impact),
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very cool, but I feel like just relying on the size of the marker to encode the (relative) impact response is clearer to see. The colormap for impact is pretty close to the one for opinions (and we may not be able to find two maps that are completely orthogonal) - so they clash a little. And I don't think we (want to) have two separate color maps for opinion and impact either.

So maybe just size? For extra happiness, you could consider using emojis instead of circles: https://community.plotly.com/t/i-want-to-use-custom-icon-for-the-scatter-markers-how-to-do-it/6644/6 (not sure if that would still be able to scale though, so maybe not)

@alyssadai alyssadai removed their request for review May 23, 2024 20:16
@michellewang michellewang merged commit 9ef62bb into neurodatascience:main May 23, 2024
1 check passed
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

Successfully merging this pull request may close these issues.

Create function to generate Plotly cloropleth map of US states & clusters
4 participants