So I have been trying to get it so there is a label at the end of each line giving the name of the country, then I can remove the legend. Have tried playing with transform_filter
but no luck.
I used data from here https://ourworldindata.org/coronavirus-source-data I cleaned and reshaped the data so it looks like this:-
index days date country value
0 1219 0 2020-03-26 Australia 11.0
1 1220 1 2020-03-27 Australia 13.0
2 1221 2 2020-03-28 Australia 13.0
3 1222 3 2020-03-29 Australia 14.0
4 1223 4 2020-03-30 Australia 16.0
5 1224 5 2020-03-31 Australia 19.0
6 1225 6 2020-04-01 Australia 20.0
7 1226 7 2020-04-02 Australia 21.0
8 1227 8 2020-04-03 Australia 23.0
9 1228 9 2020-04-04 Australia 30.0
import altair as alt
countries_list = ['Australia', 'China', 'France', 'Germany', 'Iran', 'Italy','Japan', 'South Korea', 'Spain', 'United Kingdom', 'United States']
chart = alt.Chart(data_core_sub).mark_line().encode(
alt.X('days:Q'),
alt.Y('value:Q', scale=alt.Scale(type='log')),
alt.Color('country:N', scale=alt.Scale(domain=countries_list,type='ordinal')),
)
labels = alt.Chart(data_core_sub).mark_text().encode(
alt.X('days:Q'),
alt.Y('value:Q', scale=alt.Scale(type='log')),
alt.Text('country'),
alt.Color('country:N', legend=None, scale=alt.Scale(domain=countries_list,type='ordinal')),
).properties(title='COVID-19 total deaths', width=600)
alt.layer(chart, labels).resolve_scale(color='independent')
This is the current mess that the chart is in.
How would I go about just showing the last 'country' name?
Here is the result. I might look at adjusting some of the countries separately as adjusting as a group means that some of the labels are always badly positioned no matter what I do with the dx
and dy
alignment.
You can do this by aggregating the x and y encodings. You want the text to be at the maximum x value, so you can use a 'max'
aggregate in x. For the y-value, you want the y value associated with the max x-value, so you can use an {"argmax": "x"}
aggregate.
With a bit of adjustment of text alignment, the result looks like this:
labels = alt.Chart(data_core_sub).mark_text(align='left', dx=3).encode(
alt.X('days:Q', aggregate='max'),
alt.Y('value:Q', aggregate={'argmax': 'days'}, scale=alt.Scale(type='log')),
alt.Text('country'),
alt.Color('country:N', legend=None, scale=alt.Scale(domain=countries_list,type='ordinal')),
).properties(title='COVID-19 total deaths', width=600)