Dynamic calculation of color on map based on selection
See original GitHub issueWe 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:
- Created 4 years ago
- Comments:12 (5 by maintainers)
Top GitHub Comments
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 !
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.