我借鉴了这篇关于如何使用 R 可视化李克特响应的优秀条目:
[ https://stats.stackexchange.com/questions/25109/visualizing-likert-responses-using-r-or-spss ]
尽管问题的答案非常有帮助,但我无法在一个情节中比较组。(如果这不起作用)如果您帮助我将多个图组合成一个整体图,我将不胜感激。
非常感谢!
#需要的包裹#
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)
#生成类似于我使用的样本数据的代码,即通过 5 点李克特量表 (col2) 测量的多个陈述项目 (col1),具有分组变量 (col3) 和频率响应 (col4; I设置 mean 和 sd 使数字为正)。#
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)
#准备资料#
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"
#阴谋#
items<-mydata[, c("Strongly disagree", "Disagree", "Agree", "Strongly agree")]
itemsg=likert(items, grouping =mydata$col3)
plot(itemsg)
问题:代码产生一个单一的情节,但不进行组间比较。似乎它绘制了在mydata中出现的每个项目,因此如果我们设法重新排序行,我们可能能够生成一个允许在项目和组之间进行轻松比较的图。
> 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
>