绘制Likert缩放问题并对其进行分组

时间:2014-08-05 15:53:25

标签: r

我想创建一个图,如下所示:https://stats.stackexchange.com/a/25156,堆积条形图类型。

我一直试图让它在整个下午使用Likert包。

我确实得到了一个情节,但我无法通过前/后分组。

这是一个数据样本:

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")

到目前为止,我已经采取了以下措施:

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)

无论如何,我想尝试的下一个是:

likert(测试,分组=数据$调查)

哪个不起作用,我读到它只是不再工作了,你必须在文件中乱七八糟地对它进行排序。

此外,我还发现它并没有识别出所有级别的数据(有些缺失)。我将代码修改为thing <- likert(test, nlevels = 5),但它没有修复它。

所以我的问题是,有更简单的方法吗?我在互联网上找到的所有问题/答案都是一年或更长时间,从那时起发生的事情可能会让这更直接吗?

1 个答案:

答案 0 :(得分:2)

看起来cast的行为可能已经改变,这导致了grouping=的问题。看来你可以用这个黑客修复它

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

基本上我们只是添加对as.matrix()的调用。然后用分组参数调用函数我得到

enter image description here