gpt4 book ai didi

r - Facet Wrap ggplot geom_col 具有不同的条宽

转载 作者:行者123 更新时间:2023-12-01 22:10:19 30 4
gpt4 key购买 nike

我正在使用 geom_col 创建一个包含条形图的图表,并使用 gem_point(线)将性能与“基准”进行比较。性能指标属于不同的域,因此我使用 facet_wrap 以可视方式将域分成组,以便于查看。但是,由于每个域的度量数量不同,条形“高度”/宽​​度也不同。

    df = data.frame(
measure = c("Measure A","Measure B","Measure C","Measure D","Measure E","Measure F","Measure G"),
domain = c("Efficiency","Efficiency","Satisfaction","Satisfaction", "Satisfaction","Satisfaction","Satisfaction"),
overall = c(56, 78, 19, 87, 45, 51, 19),
company = c(45, 89, 18, 98, 33, 55, 4)
)

ggplot(df %>% mutate(fill = ifelse(overall > company, " Below Overall "," Above Overall ")), aes(measure)) +
geom_col(aes(y=company, fill= fill)) + geom_point(aes(y=overall, color="overall"), size=6, shape=124) + coord_flip() +
scale_color_manual(values=c("grey3"),labels=c("Overall")) + scale_fill_manual(values=c(" Below Overall "="lightpink2"," Above Overall "="lightblue2")) + facet_wrap(~domain, ncol=1, scales="free_y")

enter image description here

我从一个较旧的问题中看到关于计算每个域的条数,然后将宽度乘以一个因子。但我不知道如何做到这一点并将其应用于我的图表。

最佳答案

也许使用 facet_gridspace = "free" 会令人满意:

ggplot(df %>% mutate(fill = ifelse(overall > company, " Below Overall  "," Above Overall  ")), aes(measure)) + 
geom_col(aes(y=company, fill= fill)) +
geom_point(aes(y=overall, color="overall"), size=6, shape=124) +
scale_color_manual(values=c("grey3"),labels=c("Overall")) +
scale_fill_manual(values=c(" Below Overall "="lightpink2"," Above Overall "="lightblue2")) +
facet_grid(domain~., scales="free", space = "free") +
coord_flip()

enter image description here

关于r - Facet Wrap ggplot geom_col 具有不同的条宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48331159/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com