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:
Our questions are the following:
Thank you.
I don't totally understand what you mean by dynamic calculation of color on map based on selection, but I can answer your other questions:
transform_lookup is effectively a left-join, so it is not symmetricFor this kind of thing, it's often easier to figure out what's going on using the data inspectors at http://vega.github.io/editor (create your chart and then click (⋯)->Open In Vega Editor)
Thanks Jake for response,
By dynamic calculation I mean that when we select bar in bar chart (which corresponds to specific age group), user data will be filtered also for map (we can do it using transform_filter) and map colors and tooltip information will be updated to user counts of filtered information. And my question about the last step how to implement it using altair.
Yeah, that should be possible.
"no, to plot a map you do not need to pass geodata as a chart argument, but if you pass it in via a lookup transform you need to be sure that you extract all the fields necessary to create a map." - how can we extract all fields for geo data? This configuration is not working for me.
Jake, maybe these questions are not for github issue, but I didn't find information about it, maybe it will be better to ask in Slack channel and close the issue?
how can we extract all fields for geo data?
You can go to the vega editor data explorer, see what fields are used in successfully creating a map, and then tweak the spec until your lookup transform successfully grabs all of those fields. I'm sure there's an easy answer, but I've never tried it before.
You didn't find any information on it because there is no information on it. Welcome to the cutting edge!
Ok, thank you Jake!
I think I did something similar before, this issue might give some hints: https://github.com/altair-viz/altair/issues/1357
@mattijn, thanks a lot for the link!
If either of you is inclined to submit a documentation PR covering these topics, that would be great
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.
Most helpful comment
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 !