question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic calculation of color on map based on selection

See original GitHub issue

We are trying to solve the task of changing color/tooltip text dynamically on map based on selection on another chart. For example, we have user data with their location:

id | country | sub-region | region | user_id
250 | France | Western Europe | Europe | 0
356 | India | Southern Asia | Asia | 1
276 | Germany | Western Europe | Europe | 2

We can create map with color based on users count grouped by country/sub-region/region, if I precalculate these values using pandas. And we can show this count in tooltip. We have additional data by user as columns (e.g., age group), and want to create also bar chart and select specific bar(s) for filtering geo map values (color + tooltip), but we can’t understand how to do it.

If we try to do something like when we are showing only map but try to add join aggregate for dynamic calculation of users’ count:

base = alt.Chart(countries).transform_joinaggregate(
    counter='count(*)',
    groupby=['country']
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(df, 'id', df.columns.tolist()),
)

base.mark_geoshape().encode(
    color='counter:Q'
)

Then we have count = 1 for all users by except for null, which is incorrect. If we try to swap countries and df (user data) in code, and try to plot bar, it is calculated correctly:

base = alt.Chart(resp_pivot_df[cols]).transform_joinaggregate(
    counter='count(*)',
    groupby=['country']
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(countries, 'id'),
)

base.mark_bar().encode(
    x='counter:Q',
    y='country'
)

But then if we try to plot map using mark_geoshape, it is not displayed. So as we understand from these experiments:

  • If we want to plot map, we need to pass geodata as Chart argument
  • transform_lookup is not symmetrical

Our questions are the following:

  • Are our conclusions correct?
  • transform_lookup is working like left join?
  • Is it possible to implement our goal (Dynamic calculation of color on map based on selection)?

Thank you.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
labdmitriycommented, Nov 15, 2019

I’ve created working example of using solution from #1357 here: https://www.kaggle.com/labdmitriy/kaggle-survey-2019-map-mini-dashboard-altair. Thanks @mattijn !

2reactions
labdmitriycommented, Nov 19, 2019

I’ve tested behavior of the example that I’ve created and understand, that partly it is not working as expected, and lookup_transform is similar but not left join as I understand, it will lookup the first record by lookup key and will continue with the next key, not created all possible pairs, so I suppose this is the main reason why dynamic aggregation is not working in my example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to: Dynamic Map Coloration with Parameters in Tableau ...
First, click the top Longitude tab in the Marks card. When it opens, you should still see your metric value field there. Instead...
Read more >
dynamic color change in map based on dimension values
I have two dimensions called Category and Sub-category. I need to change the diverging color of the map based on what i select...
Read more >
Filled map - color based on user selection
If you are work with a matrix/table or normal bar/column chart, you can achieve dynamic color control based on conditional color formatting ...
Read more >
Dynamic Quantile Map Coloring in Tableau Desktop
Create a calculated field that will bucket your data into the desired quantile based on the parameter. You'll notice that the Quantile Color...
Read more >
How to dynamically adapt the color range of a map to the countries ...
I have a map with say, the GDP per country. The color range goes from white to green. This makes countries with similar...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found