0

I have a dataset with 6 Questions and 6 Answers ranging from 1 (very bad) to 6 (very good). This can be visualized using a likert plot. Let's assume the data looks like this:

d1<-data.frame(1=c(1,2,3,4,5,NA),2=c(1,2,3,4,5,6),3=c(1,2,3,4,5,6),4=c(1,2,3,4,5,6),5=c(1,2,3,4,5,6),6=c(1,2,3,NA,5,6))

To use likert i have to transform all numbers into factors, and somehow it only works when I transform the numbers into letters first

d1[d1[1:6,1:6]==1]<-"Level 1"
d1[d1[1:6,1:6]==2]<-"Level 2"
d1[d1[1:6,1:6]==3]<-"Level 3"
d1[d1[1:6,1:6]==4]<-"Level 4"
d1[d1[1:6,1:6]==5]<-"Level 5"
d1[d1[1:6,1:6]==6]<-"Level 6"

and then defining them as factors:

d1<-data.frame(E1=as.factor(d1$1),E2=as.factor(d1$2),E3= as.factor(d1$3),E4=as.factor(4), E5=as.factor(d1$5),E6=as.factor(d1$6))

Then is use the likert package, like so:

d2<-likert(d1)
plot(d2, ordered=F)

The output is something like this (different values) enter image description here All good and well, however, I need to include percentages for each bar. I have found the option plot.percents, which can be set to TRUE, however it does not work and isn't shown as an option even though it is written in the documentation of likert. I have also seen this post:here, I tried it and it says there are too less dimensions ... I assume his code is made for 5 possible answers, whereas for me it's 6.

Lukas
  • 223
  • 3
  • 12

1 Answers1

2

Seems there is some confusion between likert and HH packages both using likert(). Probably all your variables should have the same number of factor levels (6 in this case).

Otherwise @digEmAll's code seems to be working well even with your project.

First, just save the original labels.

yLabels <- colnames(df1)

It may help then to adapt the function in line 40 by round(., 0) or something:

DF$perc <- round(ave(DF$abs, DF$y, FUN=function(x) x/sum(x) * 100), 0)  # line 40

Then, to combine the both likert()s just reference to each package seperately by ::.

library(likert)
library(HH)
HH::likert(likert::likert(df1),
           scales = list(y = list(labels = yLabels)),
           main="My Plot",
           panel=myPanelFunc,  # @digEmAll's fun applied
           sub="Response")

Result

enter image description here

Data

df1 <- data.frame(matrix(rep(paste("Level", 1:6), 6), ncol=6))
df1[6, 1] <- df1[4, 6] <- NA

> df1
       X1      X2      X3      X4      X5      X6
1 Level 1 Level 1 Level 1 Level 1 Level 1 Level 1
2 Level 2 Level 2 Level 2 Level 2 Level 2 Level 2
3 Level 3 Level 3 Level 3 Level 3 Level 3 Level 3
4 Level 4 Level 4 Level 4 Level 4 Level 4    <NA>
5 Level 5 Level 5 Level 5 Level 5 Level 5 Level 5
6    <NA> Level 6 Level 6 Level 6 Level 6 Level 6
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thanks! I'll try that and report back if it works and accept your answer! – Lukas Jul 19 '18 at 19:40
  • If my answer was helpful in the end, please consider accepting it. This signals to other users that the problem is solved and they won't invest any more time in it. – jay.sf Aug 01 '18 at 08:22