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

时间:2013-11-18 20:55:14

标签: r

我使用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"))

我错过了什么吗?

4 个答案:

答案 0 :(得分:4)

根据功能来源,分组分析目前不支持打印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

我解决了这个问题并向杰森提交了拉取请求。在此期间,您可以从此处提取更改:https://github.com/aseidlitz/likert

答案 1 :(得分:0)

嘿,我试了一下,使用分组数据对我不起作用。尽管plot.percent.lowplot.percent.high工作正常,但没有提及原因。除非其他人破解,否则我只能使用plot()代替likert.bar.plottext()提供解决方法

此处,我仅为所有三个组标记Agree类别。

plot(likert.df.group, type="bar")
text(c(0.35,0.35,0.35), c(0.85,0.6,0.25), 
     labels = paste0(c(42.8,28.57,42.85),"%") )

enter image description here

答案 2 :(得分:0)

如果您不想打扰修改源材料,我会根据源代码编写一个小插件。只需采取上述答案并应用它。如果你用它制作了很多图表,就不应该太难加入用户功能。我一直在做一些尝试添加百分比的工作,然后想办法在图表上的某处添加N.

library(likert)
library(reshape)
library(plyr)


#--------------- Works using likert package, problems with the modded source code)

rm(list=ls(all=T))

# ---------------- Example Data -------------------- #

likert.responses <- c("Agree", "Neutral", "Strongly agree", "Disagree", "Strongly disagree", NA)
questions <- c("Q_1","Q_2","Q_3")
groupA <- c("White", "Afr. American", "Hispanic", "Other")

set.seed(12345)

mydata <- data.frame(
                    race = sample(groupA, 100, replace=T, prob=c(.3,.3,.3,.01)),
                    Q_1 = sample(likert.responses, 100, replace=T, prob=c(.2,.2,.2,.2,.19,.01)),
                    Q_2 = sample(likert.responses, 100, replace=T, prob=c(.1,.2,.2,.29,.2, .01)),
                    Q_3 = sample(likert.responses, 100, replace=T, prob=c(.4,.2,.09,.15,.15,.01))
                    )


mydata.que <- mydata[questions]
mydata.que[] <- lapply(mydata.que, factor, 
                     levels=c("Strongly disagree", "Disagree", "Neutral", "Agree","Strongly agree"))


mydata.1 <- likert(mydata.que)
mydata.group <- likert(mydata.que, grouping=mydata$race)


p <- plot(mydata.group, centered=F, # This controls stacked versus the "centered" option
          ordered=F,
          plot.percents = TRUE
          ) + ggtitle("Likert Test")


# --- Gets the percentages from the likert object -- #
results <- mydata.group$results
results <- reshape::melt(results, id=c('Group', 'Item'))
results$variable <- factor(results$variable, ordered=TRUE)

lpercentpos <- ddply(results[results$value > 0,], .(Group, Item), transform, 
                                 pos = cumsum(value) - 0.5*value)

lpercentpos <- subset(lpercentpos, variable != "Neutral" & value != 100 & value != 0)


# -- Double checking percents are right -- #                                 
prop.table(table(mydata$race, mydata$Q_1),1)



pworks <-  p + geom_text(data=lpercentpos, aes(x=Group, y=pos, label=paste0(round(value), '%'),
                                    group=Item),
                                    size=3)

pworks

# --- Using the OP's code --- # 

p <- plot(likert.df.group, centered=F, # This controls stacked versus the "centered" option
          ordered=F,
          plot.percents = TRUE
          ) + ggtitle("Likert Test")


results <- likert.df.group$results
results <- reshape::melt(results, id=c('Group', 'Item'))
results$variable <- factor(results$variable, ordered=TRUE)

lpercentpos <- ddply(results[results$value > 0,], .(Group, Item), transform, 
                                 pos = cumsum(value) - 0.5*value)

lpercentpos <- subset(lpercentpos, variable != "Neutral" & value != 100 & value != 0)

prop.table(table(likert.df.group$race, likert.df.group$Q_1),1)



pworks <-  p + geom_text(data=lpercentpos, aes(x=Group, y=pos, label=paste0(round(value), '%'),
                                    group=Item),
                                    size=3)

pworks

答案 3 :(得分:0)

即使使用sys.argv[1:]数据的Likert软件包文档中包含的示例脚本也无法正确绘制百分比标签。运行此代码后,它最终看起来像下面的图片。

argv

enter image description here