Altair: Sorting faceted “text” chart not reflecting expectation

可紊 提交于 2021-02-11 09:13:36

问题


This is a direct follow up to Sorting based on the alt.Color field in Altair using the same dataframe (that is included for ease of reference). I asked a follow up in the comments section, but after giving it a whirl on my own and getting close, I am creating a new question.

Unnamed: 0,Species,Unknown,group,LDA Score,p value
11,a,3.474929757,bad,3.07502591,5.67e-05
16,b,3.109308852,bad,2.739744898,0.000651725
31,c,3.16979865,bad,2.697247855,0.03310557
38,d,0.06730106400000001,bad,2.347746497,0.013009626000000002
56,e,2.788383183,good,2.223874347,0.0027407140000000004
65,f,2.644346144,bad,2.311106698,0.00541244
67,g,3.626001112,good,2.980960068,0.038597163
74,h,3.132399759,good,2.849798377,0.007021518000000001
117,i,3.192113412,good,2.861299028,8.19e-06
124,j,0.6140430960000001,bad,2.221483531,0.0022149739999999998
147,k,2.873671544,bad,2.390164757,0.002270102
184,l,3.003479213,bad,2.667274876,0.008129727
188,m,2.46344998,good,2.182085465,0.001657861
256,n,0.048663767,bad,2.952260299,0.013009626000000002
285,o,2.783848855,good,2.387345098,0.00092491
286,p,3.636218971,good,3.094046639,0.001584756

The follow up question was after grouping by "color", how can I do a subsequent ordering within the groups by "LDA Score" or essentially by bar length and have the text column sort by LDA, as well. I didn't know how to incorporate a second level or ordering in the code I was using, so I opted to turn the groups into facets and then try sorting by LDA Score for both the bar charts and the text column. I am getting the proper sorting by LDA score on the charts, but I can't seem to make it work for the text column. I am pasting the code and the image. As you can see, I am telling it to use LDA Score as the sorting field for the "text" chart (which is the pvalue), but it is still sorting alphabetically by species. Any thoughts? To be honest I feel like I'm heading down the rabbit hole where I'm forcing a solution to work in the current code, so if you think a different strategy altogether is the better way to go, let me know.

FYI, there are some formatting issues (like redundant labels on axes) that you can ignore for now.

bars = alt.Chart(df).mark_bar().encode(
    alt.X('LDA Score'),
    alt.Y("Species:N", sort='-x'),
    color='group:N',
    row='group:N'
).resolve_scale(y='independent'
)

text = alt.Chart(df).mark_text().encode(
    alt.Text('p value:Q', format='.2e'),
    alt.Y('Species:N', sort=alt.EncodingSortField(field='LDA Score', op='count', order='descending')),
    row='group:N'
).resolve_scale(y='independent'
).properties(width=50)

#bars | text
alt.hconcat(bars, text, spacing=0)


回答1:


Drop op="count". The count in each row is exactly 1 (there is one data point in each row). It sounds like you want to instead sort by the data value.

It also would make sense in this context to use this same sort expression for both y encodings, since they're meant to match:

y_sort = alt.EncodingSortField(field='LDA Score', order='descending')

bars = alt.Chart(df).mark_bar().encode(
    alt.X('LDA Score'),
    alt.Y("Species:N", sort=y_sort),
    color='group:N',
    row='group:N'
).resolve_scale(
    y='independent'
)

text = alt.Chart(df).mark_text().encode(
    alt.Text('p value:Q', format='.2e'),
    alt.Y("Species:N", sort=y_sort, axis=None),
    alt.Row('group:N', header=alt.Header(title=None, labelFontSize=0))
).resolve_scale(
    y='independent'
).properties(width=50)

alt.hconcat(bars, text, spacing=0)

(labelFontSize is a workaround because there is a bug with labels=False)



来源:https://stackoverflow.com/questions/60640969/altair-sorting-faceted-text-chart-not-reflecting-expectation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!