多个李克特图的for循环返回“无" [英] A for loop for multiple likert graphs returns NOTHING
问题描述
我建立了大量的Likert和其他图形来评估问卷的总结果.
I set up a large amount of likert and other graphs to evaluate the total results of my questionnaire.
现在我想通过分组变量"group"来做同样的事情
Now I would like to do the same thing by a grouping variable "group"
我设置了我所有的Likert项目及其定义(我的数据格式糟糕透了,所以我不得不用R重新格式化).代码的想法如下: 1.根据likert()函数的需要对问题进行分组和重命名 2.让R根据11组划分数据帧 3.为每个组绘制一个李克特图
I set up all of my likert items and their definitions (my data is in a god-awful format, so I had to reformat it with R). The idea for the code is the following: 1. group and rename the questions according to the likert () functions needs 2. have R split the dataframe according to 11 groups 3. plot a likert graph for each of these groups
现在这应该是解决方案(它适用于条形图,但由于某些原因不适用于李克特图)
Now this should be the solution (it worked for the barplots but for some reason is not working for the likert plots):
for(i in 1:11)
{
x<-F5[which(F5$group==i),]
plot(likert(x[1:9]),low.color="red3", high.color="forestgreen",
include.center=T, plot.percents=T,ordered=T,
legend= paste("F5","Gruppe",i),
legend.position="bottom",
plot.percent.low=F,plot.percent.high=F)+ylab(ylab)+ggtitle(title.F5)
}
当我运行此代码时,完全没有发生.没有绘制李克特图,没有错误消息等.
when I run this code exactly NOTHING happens. No likert graphs are plotted, no error messages etc.
示例数据,这样每个人都可以参与其中:
EXAMPLE DATA so everyone can join the fun:
M1 M2 M3 M4 M5 M6 M7 M8 M9 group
1 1 5 5 1 2 4 4 -9 5 1
2 2 4 5 1 2 4 4 1 5 1
3 3 3 5 1 2 4 3 1 3 1
4 1 5 5 1 2 4 4 -9 5 1
5 2 4 5 1 2 4 4 1 5 2
6 1 5 5 1 2 4 4 -9 5 2
7 2 4 5 1 2 4 4 1 5 2
8 3 3 5 1 2 4 3 1 3 3
9 4 5 5 1 2 4 -9 1 3 3
10 5 5 -9 1 3 4 4 2 -9 3
11 3 3 5 1 2 4 3 1 3 3
12 4 5 5 1 2 4 -9 1 3 4
13 5 5 -9 1 3 4 4 2 -9 3
14 5 5 -9 1 3 4 4 2 -9 3
15 3 3 5 1 2 4 3 1 3 4
16 1 5 5 1 2 4 4 -9 5 4
17 2 4 5 1 2 4 4 1 5 4
18 1 5 5 1 2 4 4 -9 5 4
19 2 4 5 1 2 4 4 1 5 4
20 3 3 5 1 2 4 3 1 3 4
-
将数据从数字更改为李克特式的协议项
Change data from numeric to likert-style items of agreement
DATA[,1:9][DATA[,1:9]==1]<-"strongly agree"
DATA[,1:9][DATA[,1:9]==2]<-"agree"
DATA[,1:9][DATA[,1:9]==3]<-"unsure"
DATA[,1:9][DATA[,1:9]==4]<-"disagree"
DATA[,1:9][DATA[,1:9]==5]<-"strongly disagree"
DATA[,1:9][DATA[,1:9]==-9]<-NA
设置协议规模
Set agreement scale
agr<-c("stronlgy agree", "agree", "unsure", "disagree", "strongly disagree")
将每个因素定义为先前定义的比例
Define each as ordered factors to the previously defined scale
DATA[,1]=factor(DATA[,1], levels=agr, ordered=TRUE)
DATA[,2]=factor(DATA[,2], levels=agr, ordered=TRUE)
DATA[,3]=factor(DATA[,3], levels=agr, ordered=TRUE)
DATA[,4]=factor(DATA[,4], levels=agr, ordered=TRUE)
DATA[,5]=factor(DATA[,5], levels=agr, ordered=TRUE)
DATA[,6]=factor(DATA[,6], levels=agr, ordered=TRUE)
DATA[,7]=factor(DATA[,7], levels=agr, ordered=TRUE)
DATA[,8]=factor(DATA[,8], levels=agr, ordered=TRUE)
DATA[,9]=factor(DATA[,9], levels=agr, ordered=TRUE)
将标题设置为有趣
set title to sth funny
title.DATA<-由于R愚蠢,此消息不会显示."
title.DATA<-"This message will not show up because R is stupid."
编写循环并观看R没什么
program a loop and watch R do NOTHING
for(1:11中的i) { x< -DATA [which(DATA $ group == i),] 情节(likert(x [1:9]),low.color ="red3",high.color ="forestgreen", include.center = T,plot.percents = T,ordered = T, 图例= paste("DATA","Gruppe",i), legend.position ="bottom", plot.percent.low = F,plot.percent.high = F)+ ylab(ylab)+ ggtitle(title.DATA) }
for(i in 1:11) { x<-DATA[which(DATA$group==i),] plot(likert(x[1:9]),low.color="red3", high.color="forestgreen", include.center=T, plot.percents=T,ordered=T, legend= paste("DATA","Gruppe",i), legend.position="bottom", plot.percent.low=F,plot.percent.high=F)+ylab(ylab)+ggtitle(title.DATA) }
推荐答案
像这样对我有效.我最近遇到了这个问题.图形图会自动循环打印,而网格图则不会.对于他们,您必须添加print
.
It works for me like that. I came across this problem myself recently. graphics plots print automatically in loops, grid plots don't. For them you have to add print
.
library(likert)
for(i in 1:4) { x<-DATA[which(DATA$group==i),]
lp <- plot(likert(x[1:9]),low.color="red3",
high.color="forestgreen", include.center=T,
plot.percents=T,ordered=T, legend= paste("DATA","Gruppe", i),
legend.position="bottom", plot.percent.low=F,plot.percent.high=F) +
ylab("ylab") +
ggtitle("This message will not show up because R is stupid.")
print(lp)
}
这篇关于多个李克特图的for循环返回“无"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!