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

后端 未结 4 1338
被撕碎了的回忆
被撕碎了的回忆 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:14

    I wrote a little add-on based off the source code, if you don't want to bother modding the source material. Just takes the answer above and applies it. Shouldn't be too hard to put into a user function if you make a lot of graphs with it. I have been doing some work trying to get the percents added and then figure a way to add the N's somewhere on the graph.

    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
    

提交回复
热议问题