CF <- split(JG,JG$itemID) #将数据框按题目分割,每个题目做一个图 for(i in 1:40){ #i=4 ############################## grid作图用 TM <- as.data.frame(CF[i]) colnames(TM) <- names(JG) TM <- TM[,-1] tm <- as.data.frame(gather(TM,TM,value,-option)) ### 转数据 tm[,2] <- as.numeric(tm[,2]) ### 字符转数值 tm[,3] <- as.numeric(tm[,3]) B <- names(CF[i]) dir<-paste("图片/",B,".png",sep = "") png(dir) ######### 准备画图 ######### p <- ggplot(tm,aes(TM,value,colour=option,linetype= option))+ geom_text(aes(label= option))+ geom_smooth(span=1,se=F)+ggtitle(B)+ coord_cartesian(ylim = c(0,100)) + labs(y=\'Smoothed % Chosing\',x=\'Criterion Score\')+ guides(colour= guide_legend(title = \'choice\'),linetype = "none")+ geom_vline(xintercept = c(0.6,0.8,0.9),linetype="dotdash")+theme_bw() print(p) dev.off() }
将多个图合并
library(gridExtra)
grid.arrange(p, p2, p3, p4)