gpt4 book ai didi

r - ggplot2:当类别低于零时,堆积条形标签显示在不正确的位置和顺序

转载 作者:行者123 更新时间:2023-12-02 02:54:51 25 4
gpt4 key购买 nike

我正在尝试创建大量发散条形图 (like those found here)在 R 中使用 ggplot2。但是,我无法让不需要的类别(堆叠在 0 以下)的标签以正确的顺序和正确的位置打印。这是一些可重现的代码:

foo <- data.frame(value=c(2:6)
,percent=c(-.185, -.074, .148, .074, .518)
,col=c("#EF8A62", "#FDDBC7", "#D1E5F0", "#67A9CF", "#2166AC")
,set="Expectations")

semanticLevels <- c("Very Negative"
,"Negative"
,"Slightly Negative"
,"Slightly Positive"
,"Positive"
,"Very Positive")

pal <- brewer.pal(6,"RdBu")

ggplot() + geom_bar(data=foo, aes(x = set, y=percent, fill=col), position="stack", stat="identity") +
geom_hline(yintercept = 0, color =c("white")) +
geom_text(data=foo, aes(x = set, y=percent, label=scales::percent(abs(percent))), position = position_stack(vjust = .5), angle=-90) +
scale_fill_identity("", labels = semanticLevels, breaks=pal, guide=guide_legend(nrow=1)) +
coord_flip() +
scale_y_continuous(breaks=seq(-1,1,.5), limits=c(-1,1),labels=scales::percent) +
theme(legend.position = "bottom" )

这会产生下图:

Stacked (diverging) bar graph with misplaced data labels

如您所见,理想类别的所有数据标签都集中在其适当的类别中,但对于零以下的不良类别,数据标签是乱序的并且未在其各自的类别中居中。

我已经尝试了很多解决这个问题的方法,但还没有想出任何可以解决这个问题的简单改变。

有没有人有什么想法?

最佳答案

由于某些原因 ggplot 会在您分别调用每一层时弄乱堆叠,但如果您允许继承数据和美学效果,则可以正常工作:

foo <- data.frame(value=c(2:6)
,percent=c(-.185, -.074, .148, .074, .518)
,col=c("#EF8A62", "#FDDBC7", "#D1E5F0", "#67A9CF", "#2166AC")
,set="Expectations")

semanticLevels <- c("Very Negative"
,"Negative"
,"Slightly Negative"
,"Slightly Positive"
,"Positive"
,"Very Positive")

pal <- brewer.pal(6,"RdBu")

ggplot(data=foo,aes(x = set, y=percent, fill=col)) +
geom_bar(position="stack", stat="identity") +
geom_hline(yintercept = 0, color =c("white")) +
geom_text(aes(label=scales::percent(abs(percent))), position = position_stack(vjust = .5), angle=-90) +
scale_fill_identity("", labels = semanticLevels, breaks=pal, guide=guide_legend(nrow=1)) +
coord_flip() +
scale_y_continuous(breaks=seq(-1,1,.5), limits=c(-1,1),labels=scales::percent) +
theme(legend.position = "bottom" )

Done with inheritance

如果你想要一个更令人满意(并且可能更可靠)的解决方案,你可以自己指定定位:

library(dplyr)

foo <- foo %>%
group_by_all() %>%
mutate(position = ifelse(value<=3,sum(foo$percent[foo$value>value & foo$value<=3])
,sum(foo$percent[foo$value<value & foo$value>3]))+.5*percent)

ggplot() +
geom_bar(data=foo, aes(x = set, y=percent, fill=col), position="stack", stat="identity") +
geom_text(data=foo, aes(x = set, y=position, label=scales::percent(abs(percent))), position = "identity", angle=-90) +
geom_hline(yintercept = 0, color =c("white")) +
scale_fill_identity("", labels = semanticLevels, breaks=pal, guide=guide_legend(nrow=1)) +
coord_flip() +
scale_y_continuous(breaks=seq(-1,1,.5), limits=c(-1,1),labels=scales::percent) +
theme(legend.position = "bottom" )

Done with explicit position

关于r - ggplot2:当类别低于零时,堆积条形标签显示在不正确的位置和顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50012072/

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