1 回答
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
在大多數(shù)情況下,當(dāng)您發(fā)現(xiàn)自己與圖例作斗爭(zhēng)時(shí),這表明您正在繪制的數(shù)據(jù)沒有被有意義地排列。圖例旨在幫助解釋映射變量。在您的情況下,所有這些水平線都可以用一個(gè)變量表示,即“年齡統(tǒng)計(jì)”。
然后解決方案是將它們放入數(shù)據(jù)框中并使用一次調(diào)用,geom_hline以便繪圖系統(tǒng)可以處理圖例。
sdf = pd.DataFrame({
'age_statistic': [
'mean', 'median', IQR,
'10th Percentile', '90th Percentile',
'std'
],
'value' : [
test.age.mean(), test.age.median(), IQR,
test['age'].quantile(0.1), test['age'].quantile(0.9),
test['age'].std()
]
})
(ggplot(...)
...
+ geom_hline(sdf, aes(yintercept='value', colour='age_statistic'), show_legend=True)
)
添加回答
舉報(bào)
