英文:
plotnine:: is there a better way to refer args of facet_wrap in after_stat
问题
以下是您要的内容的翻译:
"I wants to show percentages for bar plots using plotnine
with facet_wrap
and stat = 'count'
.
(Of course I can do it with preparation of values and stat = 'identity'
, but I want to avoid it.)
When I give the arg of facet_wrap
to aes
, I can refer it in after_stat
.
But it needs to nullify the aes
manually. it seems ridiculous.
Is there a better way to do it ?
Any help would be greatly appreciated. Below is an example;
from plotnine import *
from plotnine.data import mtcars
import pandas as pd
def prop_per_xcc(x, color, count):
df = pd.DataFrame({'x': x, 'color': color, 'count': count})
prop = df['count']/df.groupby(['x', 'color'])['count'].transform('sum')
return prop
facet_num = mtcars.vs.nunique()
print(
ggplot(mtcars, aes('factor(cyl)', fill='factor(am)')) +
geom_bar(position='fill') +
geom_text(aes(color = "factor(vs)", # sets arg of facet wrap to refer in after_stat
label = after_stat('prop_per_xcc(x, color, count) * 100')),
stat = 'count',
position = position_fill(vjust = 0.5),
format_string = '{:.1f}%',
show_legend = False) +
scale_color_manual(values = ["black"] * facet_num) + # nullify the aes manually
facet_wrap("vs")
)
[![enter image description here][1]][1]"
<details>
<summary>英文:</summary>
I wants to show percentages for bar plots using `plotnine` with `facet_wrap` and `stat = 'count'`.
(Of course I can do it with preparation of values and `stat = 'identity'`, but I want to avoid it.)
When I give the arg of `facet_wrap` to `aes`, I can refer it in `after_stat`.
But it needs to nullify the `aes` manually. it seems ridiculous.
Is there a better way to do it ?
Any help would be greatly appreciated. Below is an example;
from plotnine import *
from plotnine.data import mtcars
import pandas as pd
def prop_per_xcc(x, color, count):
df = pd.DataFrame({'x': x, 'color': color, 'count': count})
prop = df['count']/df.groupby(['x', 'color'])['count'].transform('sum')
return prop
facet_num = mtcars.vs.nunique()
print(
ggplot(mtcars, aes('factor(cyl)', fill='factor(am)')) +
geom_bar(position='fill') +
geom_text(aes(color = "factor(vs)", # sets arg of facet wrap to refer in after_stat
label = after_stat('prop_per_xcc(x, color, count) * 100')),
stat = 'count',
position = position_fill(vjust = 0.5),
format_string = '{:.1f}%',
show_legend = False) +
scale_color_manual(values = ["black"] * facet_num) + # nullify the aes manually
facet_wrap("vs")
)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/miJUW.png
</details>
# 答案1
**得分**: 1
您的想法是正确的,但您可能不了解正确的语法。您应该使用分阶段的审美评估。
```python
from plotnine import *
from plotnine.data import mtcars
import pandas as pd
def prop_per_xcc(x, label, count):
df = pd.DataFrame({'x': x, 'label': label, 'count': count})
prop = df['count']/df.groupby(['x', 'label'])['count'].transform('sum')
return prop
facet_num = mtcars.vs.nunique()
print(
ggplot(mtcars, aes('factor(cyl)', fill='factor(am)')) +
geom_bar(position='fill') +
geom_text(aes(label = stage(start="vs", after_stat='prop_per_xcc(x, label, count) * 100')),
stat = 'count',
position = position_fill(vjust = 0.5),
format_string = '{:.1f}%',
show_legend = False) +
facet_wrap("vs")
)
它表示label
映射到与分面相同的变量,然后在计算统计数据后由自定义函数进行精化。
英文:
You are thinking correctly, but just don't know about the right syntax. You should use staged aesthetic evaluation.
from plotnine import *
from plotnine.data import mtcars
import pandas as pd
def prop_per_xcc(x, label, count):
df = pd.DataFrame({'x': x, 'label': label, 'count': count})
prop = df['count']/df.groupby(['x', 'label'])['count'].transform('sum')
return prop
facet_num = mtcars.vs.nunique()
print(
ggplot(mtcars, aes('factor(cyl)', fill='factor(am)')) +
geom_bar(position='fill') +
geom_text(aes(label = stage(start="vs", after_stat='prop_per_xcc(x, label, count) * 100')),
stat = 'count',
position = position_fill(vjust = 0.5),
format_string = '{:.1f}%',
show_legend = False) +
facet_wrap("vs")
)
It says, the label
is mapped to the same variable as the facets, then refined by the custom function after the statistics are calculated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论