I draw on this excellent entry on how to visualize Likert responses using R:
[ https://stats.stackexchange.com/questions/25109/visualizing-likert-responses-using-r-or-spss ]
Although the answers to the question are extremely helpful, I do not manage to compare groups within one plot. (If this does not work) I would appreciate if you helped me to combine multiple plots into one overall graph.
Many thanks!
#Packages needed#
install.packages(c('devtools', 'roxygen2', 'RSQLite', 'ipeds','reshape'), repos=c('http://cran.r-project.org', 'http://r-forge.r-project.org'))
require(devtools)
require(roxygen2)
library(ggplot2)
library(HH)
library(reshape)
library(gridExtra)
#Code to produce sample data similar to the one I used, i.e. a number of statement items (col1) measured by a 5-point Likert scale (col2), with a grouping variable (col3) and the responses in frequencies (col4; I set mean and s.d. so that numbers are positive).#
mydata1<-expand.grid(col1=c('item1', 'item2', 'item3', 'item4'), col2=c('0', '1', '2', '3', '4'), col3=c('T1'))
m<-2:7
s<-0:1
mydata1$col4=sapply(rnorm(20,m,s), function(x) {round(x,2)})
mydata1$col2<-factor(mydata1$col2, levels=c(0,1,2,3,4), labels=c("strongly disagree", "disagree", "neutral", "agree", "strongly agree"), ordered=TRUE)
mydata1<-reshape(mydata1, direction="wide", v.names="col4", timevar="col2", idvar="col1")
mydata2<-expand.grid(col1=c('item1', 'item2', 'item3', 'item4'),col2=c('0', '1', '2', '3', '4'),col3=c('T0'))
m<-2:7
s<-0:1
mydata2$col4=sapply(rnorm(20,m,s), function(x) {round(x,2)})
mydata2$col2<-factor(mydata2$col2, levels=c(0,1,2,3,4),labels=c("strongly disagree", "disagree", "neutral", "agree", "strongly agree"), ordered=TRUE)
mydata2<-reshape(mydata2, direction="wide", v.names="col4", timevar="col2", idvar="col1")
mydata3<-expand.grid(col1=c('item1', 'item2', 'item3', 'item4'),col2=c('0', '1', '2', '3', '4'),col3=c('C1'))
m<-2:7
s<-0:1
mydata3$col4=sapply(rnorm(20,m,s), function(x) {round(x,2)})
mydata3$col2<-factor(mydata3$col2,levels=c(0,1,2,3,4),labels=c("strongly disagree", "disagree", "neutral", "agree", "strongly agree"), ordered=TRUE)
mydata3<-reshape(mydata3, direction="wide", v.names="col4", timevar="col2", idvar="col1")
mydata4<-expand.grid(col1=c('item1', 'item2', 'item3', 'item4'),col2=c('0', '1', '2', '3', '4'),col3=c('C0'))
m<-2:7
s<-0:1
mydata4$col4=sapply(rnorm(20,m,s), function(x) {round(x,2)})
mydata4$col2<-factor(mydata4$col2,levels=c(0,1,2,3,4), labels=c("strongly disagree", "disagree", "neutral", "agree", "strongly agree"), ordered=TRUE)
mydata4<-reshape(mydata4, direction="wide", v.names="col4", timevar="col2", idvar="col1")
mydata<-rbind(mydata1, mydata2, mydata3, mydata4)
summary(mydata)
#Preparation of the data#
mydata$col4.neutral<-NULL
colnames(mydata)[colnames(mydata)=="col4.strongly disagree"]<-"Strongly disagree"
colnames(mydata)[colnames(mydata)=="col4.disagree"]<-"Disagree"
colnames(mydata)[colnames(mydata)=="col4.agree"]<-"Agree"
colnames(mydata)[colnames(mydata)=="col4.strongly agree"]<-"Strongly agree"
#PLOT#
items<-mydata[, c("Strongly disagree", "Disagree", "Agree", "Strongly agree")]
itemsg=likert(items, grouping =mydata$col3)
plot(itemsg)
PROBLEM: The code produces one single plot but does not compare between groups. It seems as if it plots each item as it appears in mydata so if we manage to re-order the rows, we might be able to produce a plot that allows for easy comparison between items and groups.
> ro.mydata
col1 col3 Strongly disagree Disagree Agree Strongly agree
item1 (T1) item1 T1 2.00 6.00 2.00 6.00
item1 (T0) item1 T0 2.00 6.00 2.00 6.00
item2 (T1) item2 T1 1.90 6.59 2.67 8.33
item2 (T0) item2 T0 3.57 6.76 3.23 9.03
item3 (T1) item3 T1 4.00 2.00 4.00 2.00
item3 (T0) item3 T0 4.00 2.00 4.00 2.00
item4 (T1) item4 T1 7.02 2.66 6.31 2.76
item4 (T0) item4 T0 3.56 3.63 4.74 3.21
item1 (C1) item1 C1 2.00 6.00 2.00 6.00
item1 (C0) item1 C0 2.00 6.00 2.00 6.00
item2 (C1) item2 C1 4.01 6.87 2.62 6.23
item2 (C0) item2 C0 2.95 5.95 3.69 5.36
item3 (C1) item3 C1 4.00 2.00 4.00 2.00
item3 (C0) item3 C0 4.00 2.00 4.00 2.00
item4 (C1) item4 C1 4.10 2.54 6.12 2.62
item4 (C0) item4 C0 4.57 1.94 3.64 2.86
>