gpt4 book ai didi

r - 使用 Likert 包绘制百分比 - 分组时不起作用

转载 作者:行者123 更新时间:2023-12-04 09:48:20 24 4
gpt4 key购买 nike

我已经使用 Likert 包创建了一些图表,但是当我按组创建图时, plot.percents = TRUE 不会为我提供每个响应类别的标签。 plot.percents.high =TRUE 和 plot.percents.low = TRUE 给了我组合百分比,但是我希望它用于所有响应类别。它适用于未分组的数据。我正在使用的代码是:

制作一些数据

library(likert)
library (reshape)

Group <- c("Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 1", "Group 2", "Group 2", "Group 2", "Group 2", "Group 2",
"Group 2","Group 2", "Group 3", "Group 3", "Group 3", "Group 3","Group 3","Group 3","Group 3")

Var1 <- c("Agree", "Agree", "Strongly agree", "Agree", "Strongly disagree", "Agree","Strongly agree", "Disagree", "Strongly agree",
"Strongly agree", "Agree", "Disagree", "Agree", "Strongly disagree", "Agree", "Agree", "Agree", "Disagree", "Strongly agree",
"Strongly disagree", "Strongly agree")

df <- as.data.frame (cbind(Group, Var1))

Variable <- c("Var1")

df2 <- (df[Variable])


likert.df <- likert (df2)

likert.df.group <- likert (df2, grouping=df$Group)

likert.df 是所有人的回答,likert.df.group 是每个组的回答。当我只用likert.df 运行绘图(如下)时,我得到每个响应的百分比,当我为likert.df.group 运行它时,它们消失了。
likert.bar.plot(likert.df, low.color = "#007CC2",
high.color = "#F7971C", neutral.color = "grey90",
neutral.color.ramp = "white", plot.percent.low = FALSE,
plot.percent.high = FALSE, plot.percent.neutral = FALSE,
plot.percents = TRUE, text.size = 4,
text.color = "black", centered = FALSE,
include.center = FALSE, ordered = FALSE,
wrap.grouping = 50, legend = "Response",
legend.position = "bottom", panel.arrange = "v",
panel.strip.color = "grey90")+
ggtitle("Chart Title") +
theme (panel.background = element_rect(fill="NA")) +
theme (axis.text.y = element_text (colour="black", size="10", hjust=0))+
theme (axis.text.x = element_text (colour="black", size="10")) +
theme (legend.title = element_blank())+
theme (plot.margin = unit (c(0,0,0,0),"mm"))

我错过了什么吗?

最佳答案

根据函数源,分组分析目前不支持打印plot.percents。见 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L174

包代码有一个小问题,很容易修复(除非我忽略了其他东西)。

在线175 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L175改变:

# lpercentpos <- ddply(results[results$value > 0,], .(Item), transform, 
lpercentpos <- ddply(results[results$value > 0,], .(Group, Item), transform,

在线177 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L177改变:
#    p + geom_text(data=lpercentpos, aes(x=Group, y=pos, label=paste0(round(value), '%'),
p <- p + geom_text(data=lpercentpos, aes(x=Group, y=pos, label=paste0(round(value), '%'),

并在线 184 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L184改变:
# lpercentneg <- ddply(lpercentneg, .(Item), transform, 
lpercentneg <- ddply(lpercentneg, .(Group, Item), transform,

然后取消注释此部分并从 if 语句中删除 FALSE
 # if(FALSE & plot.percents) { #TODO: implement for grouping
if(plot.percents) {

这是 if 语句中的代码片段:
# if(FALSE & plot.percents) { #TODO: implement for grouping
if(plot.percents) {
# warning('plot.percents is not currenlty supported for grouped analysis.')
lpercentpos <- ddply(results[results$value > 0,], .(Group, Item), transform,
pos = cumsum(value) - 0.5*value)
p <- p + geom_text(data=lpercentpos, aes(x=Group, y=pos, label=paste0(round(value), '%'),
group=Item), size=text.size)
lpercentneg <- results[results$value < 0,]
if(nrow(lpercentneg) > 0) {
lpercentneg <- lpercentneg[nrow(lpercentneg):1,]
lpercentneg$value <- abs(lpercentneg$value)
lpercentneg <- ddply(lpercentneg, .(Group, Item), transform,
pos = cumsum(value) - 0.5*value)
lpercentneg$pos <- lpercentneg$pos * -1
p <- p + geom_text(data=lpercentneg, aes(x=Item, y=pos, label=paste0(round(abs(value)), '%')),
size=text.size)
}
}

我没有做过太多测试,但您的测试数据工作正常并产生以下输出:

enter image description here

我修复了这个问题并向 Jason 提交了一个拉取请求。同时,您可以从此处提取更改: https://github.com/aseidlitz/likert

关于r - 使用 Likert 包绘制百分比 - 分组时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20057697/

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