Asking questions on a scale of questions and grouping

I would like to create a plot as shown here: https://stats.stackexchange.com/a/25156 , a type of bar graph.

I've been trying to get it to work with a package like this all day.

I was able to get the plot, but I couldn't group it using pre / post.

Here's some sample 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's what I've done 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 one I wanted to try was:

likert (test, grouping = data $ Survey)

Which just didn't work, I read that it just doesn't work anymore and you have to mess around with the files to sort it.

In addition, I can also see that it does not recognize all data layers (some are missing). I changed the code to thing <- likert(test, nlevels = 5)

, but it didn't fix it.

So my question is, is there an easier way to do this? All the questions / answers I found on the internet are a year or more, has something happened since then that could make this more direct?

+3


source to share


1 answer


It looks like the behavior cast

may have changed, which is what caused the problem with grouping=

. It seems 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 just add the call as.matrix()

. Then by calling the function with the grouping parameter, I get

enter image description here

+2


source







All Articles