Plot Percents with Likert Package - Doesn`t work when grouping

后端 未结 4 1295
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 23:29

I\'ve created some charts using the Likert package, however when I create plots by groups the plot.percents = TRUE won\'t give me the labels for each response category. The

4条回答
  •  一向
    一向 (楼主)
    2021-01-19 00:23

    According to the function source, printing of plot.percents is not currently supported for grouped analysis. See https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L174

    There's a slight problem with the package code, which is easy to fix (unless I am overlooking something else).

    On line 175 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L175 change:

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

    on line 177 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L177 change:

    #    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), '%'),
    

    and on line 184 https://github.com/jbryer/likert/blob/master/R/plot.likert.bar.r#L184 change:

    # lpercentneg <- ddply(lpercentneg, .(Item), transform, 
      lpercentneg <- ddply(lpercentneg, .(Group, Item), transform, 
    

    Then uncomment this section and remove FALSE from the if statement

     # if(FALSE & plot.percents) { #TODO: implement for grouping
       if(plot.percents) { 
    

    Here's the snippet which goes inside the if statement:

    # 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)
            }
        }
    

    I haven't done much testing, but your test data works fine and produces this output:

    enter image description here

    I fixed this issue and submitted a pull request to Jason. In the meantime you can pull the changes from here: https://github.com/aseidlitz/likert

提交回复
热议问题