我已经使用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)
        }
    }

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

我已解决此问题,并向Jason提出了拉取请求。同时,您可以从此处进行更改:https://github.com/aseidlitz/likert

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

10-12 18:03