Plotting likert scale questions and grouping them

自闭症网瘾萝莉.ら 提交于 2021-02-19 03:51:12

问题


I would like to create a plot, like is shown here: https://stats.stackexchange.com/a/25156, the stacked bar chart type.

I have been trying to get it to work with the likert package all afternoon.

I did manage to get a plot, but I couldn't get it to group by pre/post.

Here's a sample of data:

data <- structure(list(ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Survey = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("pre", "post"), class = "factor"), Optimistic = structure(c(3L, 
3L, 2L, 3L, NA, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 2L, 2L, 1L, 2L, 2L, 
2L, 3L, 2L), .Label = c("All of the time", "Often", "Some of the time"
), class = "factor"), Useful = structure(c(4L, 4L, 2L, 4L, NA, 
2L, 4L, 2L, 4L, 1L, 4L, 3L, 4L, 2L, 2L, 2L, 1L, 2L, 4L, 2L), .Label = c("All of the time", 
"Often", "Rarely", "Some of the time"), class = "factor"), Relaxed = structure(c(4L, 
4L, 3L, 4L, NA, 4L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 1L, 
2L, 4L, 4L), .Label = c("All of the time", "Often", "Rarely", 
"Some of the time"), class = "factor"), Handling = structure(c(3L, 
2L, 2L, 4L, 1L, 2L, 3L, 2L, 4L, 2L, 3L, 4L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L), .Label = c("All of the time", "Often", "Rarely", 
"Some of the time"), class = "factor"), Thinking = structure(c(4L, 
2L, 4L, 4L, 1L, 2L, 3L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 1L, 2L, 2L, 
2L, 2L, 4L), .Label = c("All of the time", "Often", "Rarely", 
"Some of the time"), class = "factor"), Closeness = structure(c(3L, 
3L, 4L, 4L, 2L, 2L, 3L, 2L, 4L, 1L, 4L, 4L, 4L, 2L, 1L, 2L, 1L, 
2L, 4L, 4L), .Label = c("All of the time", "Often", "Rarely", 
"Some of the time"), class = "factor"), Mind = structure(c(3L, 
2L, 1L, 2L, 1L, 2L, 2L, 2L, 4L, 1L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 
2L, 2L, 2L), .Label = c("All of the time", "Often", "Rarely", 
"Some of the time"), class = "factor")), .Names = c("ID", "Survey", 
"Optimistic", "Useful", "Relaxed", "Handling", "Thinking", "Closeness", 
"Mind"), row.names = c(NA, -20L), class = "data.frame")

And here is what I have done to it so far:

surveylevels <- c("pre","post")  # I put this bit in when I started trying to do the grouping
data$Survey <- factor(data$Survey, levels = surveylevels)

test <- data
test$ID <- NULL  # because the likert package thing apparently can't deal with anything else :(
test$Survey = NULL # ditto

levels <- c("All of the time",  "Often",    "Some of the time", "Rarely",   "None of the time")

test$Mind <- factor(test$Mind, levels = levels)
test$Optimistic   <- factor(test$Optimistic, levels = levels)
test$Useful  <- factor(test$Useful, levels = levels)
test$Relaxed     <- factor(test$Relaxed, levels = levels)
test$Handling    <- factor(test$Handling, levels = levels)
test$Thinking    <- factor(test$Thinking, levels = levels)
test$Closeness <- factor(test$Closeness, levels = levels)

thing <- likert(test)

plot(thing)

Anyway, the next I wanted to try was:

likert(test, grouping = data$Survey)

Which just wasn't working, I read that it just doesn't work anymore and you have to mess around in the files to get it sorted.

Additionally, I also see that it isn't recognising all the levels of the data (some are missing). I amended the code to thing <- likert(test, nlevels = 5) but it did not fix it.

So my question is, is there a simpler way to do this? All the questions/answers I found on the internet are a year old or more, has anything happened since then that might make this more straight forward?


回答1:


It looks like the behavior of cast may have changed which is what was causing the problem with grouping=. It appears you can fix it with this hack

body(likert)[[c(7,3,3,4,4)]]<-quote(t <- apply(as.matrix(t), 2, FUN = function(x) {
    x/sum(x) * 100
}))

basically we are just adding in a call to as.matrix(). Then calling the function with the grouping parameter I get

enter image description here



来源:https://stackoverflow.com/questions/25143183/plotting-likert-scale-questions-and-grouping-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!