在 ggplot (plotnine) 中使用position_dodge2 分组条形图列定位,以防丢失观察结果

问题描述 投票:0回答:1

我有一个分组条形图,有 2 组。如果在某种情况下,特定列没有观测值,则该组的另一列居中。我找不到办法规避这个问题。我希望“高度 0”的列只是缺少一个空间,而不是其余的居中。

当前视觉效果

想要的视觉效果

import plotnine as pn
import pandas as pd

data = pd.read_csv("starwars.csv")  # https://github.com/tidyverse/dplyr/blob/main/data-raw/starwars.csv
hw = data["homeworld"].value_counts().head(6)
data = data.merge(hw, left_on="homeworld", right_on="homeworld")

# Local data adjustments
data = data.dropna(subset=["gender"])

# reorder: default = by x axis name, we want order by male count
order = data["homeworld"].value_counts().index.tolist()

(
    pn.ggplot(data, pn.aes(x="factor(homeworld)", fill="factor(gender, ['male', 'female'])"))
    + pn.geom_bar(position=pn.position_dodge2(preserve = "single", padding = 0))
    + pn.scale_fill_manual({"male": pf.color.BLUE, "female": pf.color.RED})
    + pn.scale_x_discrete(limits=order)
    + pn.scale_y_continuous(expand = (0,0))
    + pn.labs(
        title = "Difference between males and females", 
        y = "Count", x = "Homeworld", 
        fill = 'Gender',
        caption = "There is a significant difference between the number of males and females on Naboo and Tatooine.",
    )
)
python ggplot2 position bar-chart plotnine
1个回答
0
投票

既然你使用

padding=0
,无论如何你都可以通过切换到
position = position_dodge(preserve = "single")
来达到你想要的结果:

import plotnine as pn
import pandas as pd

data = pd.read_csv("https://raw.githubusercontent.com/tidyverse/dplyr/refs/heads/main/data-raw/starwars.csv")

hw = data["homeworld"].value_counts().head(6)
data = data.merge(hw, left_on="homeworld", right_on="homeworld")

# Local data adjustments
data = data.dropna(subset=["sex"])

# reorder: default = by x axis name, we want order by male count
order = data["homeworld"].value_counts().index.tolist()

(
    pn.ggplot(data, pn.aes(x="factor(homeworld)", fill="factor(gender, ['masculine', 'feminine'])"))
    + pn.geom_bar(position=pn.position_dodge(preserve = "single"))
    + pn.scale_fill_manual({"masculine": "blue", "feminine": "red"})
    + pn.scale_x_discrete(limits=order)
    + pn.scale_y_continuous(expand = (0,0))
    + pn.labs(
        title = "Difference between males and females", 
        y = "Count", x = "Homeworld", 
        fill = 'Gender',
        caption = "There is a significant difference between the number of males and females on Naboo and Tatooine.",
    )
)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.