问题
I set up a large amount of likert and other graphs to evaluate the total results of my questionnaire.
Now I would like to do the same thing by a grouping variable "group"
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<-"This message will not show up because R is stupid."
program a loop and watch R do NOTHING
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) }
回答1:
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)
}
来源:https://stackoverflow.com/questions/55650737/a-for-loop-for-multiple-likert-graphs-returns-nothing