gpt4 book ai didi

r - 如何在李克特条形图中不让百分比标签重叠(排斥百分比标签)?

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

我对 R 还是比较陌生,所以非常感谢任何帮助。

我创建了一个李克特条形图,但图中显示的一些百分比重叠。我想知道如何防止这种重叠发生?我阅读了有关 ggrepel 的信息,并尝试合并 ggrepel 代码但未成功。有谁知道是否存在代码要求图中显示的百分比(即来自 plot.percents=TRUE)不重叠?

这是我的代码,没有尝试合并 ggrepel:

likert.bar.plot(likert_Prevention.ALL, plot.percents=TRUE, plot.percent.high= FALSE, plot.percent.low=FALSE, 
center = 3, low.color = "grey72", high.color = "red3", neutral.color = "lightsalmon",
legend.position = "bottom", text.size=3.5)

以及我合并 ggrepel 的尝试:

likert.bar.plot(likert_Prevention.ALL, plot.percents=TRUE, plot.percent.high= FALSE, plot.percent.low=FALSE, 
center = 3, low.color = "grey72", high.color = "red3", neutral.color = "lightsalmon",
legend.position = "bottom", text.size=3.5) + labs(geom_label_repel()) + labs(geom_text_repel())

下面是当前重叠百分比的裁剪图像:

Likert bar plot with overlapping percentages

提前感谢您的帮助和时间!如果需要任何说明,请告诉我,因为这是我在 stackoverflow 上发表的第一篇文章。

最佳答案

这不是最简单的事情,我不知道飞箭是否会使它变得更复杂。这是使用示例数据的内容:

library(likert)
library(ggplot2)
library(plyr)
library(dplyr)
data(pisaitems)

items29 <- pisaitems[,substr(names(pisaitems), 1,5) == 'ST25Q']
names(items29) <- c("Magazines", "Comic books", "Fiction",
"Non-fiction books", "Newspapers")
l29 <- likert(items29)

默认是这样的:

likert.bar.plot(l29, plot.percents=TRUE, plot.percent.high= TRUE, 
plot.percent.low=TRUE, center = 3, low.color = "grey72", high.color = "red3",
neutral.color = "lightsalmon",legend.position = "bottom", text.size=3.5)

enter image description here

你画了一个没有标签的图:

p = likert.bar.plot(l29, plot.percent.neutral=FALSE, plot.percent.high= FALSE, 
plot.percent.low=FALSE, center = 3, low.color = "grey72", high.color = "red3",
neutral.color = "lightsalmon",legend.position = "bottom", text.size=3.5)

拉出负坐标、正坐标和中性坐标:

dat_pos = g$data %>% filter(value>0) %>% 
group_by(Item) %>% arrange(variable) %>%
mutate(ycoord=cumsum(value)-0.5*value) %>%
slice(2:n())

dat_neg = g$data %>% filter(value<0) %>%
group_by(Item) %>% arrange(desc(variable)) %>%
mutate(ycoord=cumsum(value)-0.5*value) %>%
mutate(value=abs(value)) %>% slice(2:n())

dat_neu = g$data %>% group_by(Item) %>%
filter(duplicated(variable)) %>%
mutate(value=2*abs(value),ycoord=0)

并将其添加到绘图中:

p + geom_label_repel(data=rbind(dat_neu,dat_pos,dat_neg),
aes(x=Item,y=ycoord,label=paste0(round(value),"%")),size=3,force=10)

enter image description here

关于r - 如何在李克特条形图中不让百分比标签重叠(排斥百分比标签)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65208008/

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