本文主要是介绍R语言画小提琴图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
R语言画小提琴图
library(foreign)
mydataframe <- read.table("C:/Users/ASUS/Desktop/题目5数据处理/剔除异常值新2.csv",header=TRUE,sep=",")
opar <- par(no.readonly=TRUE)
par(mfrow=c(2,4))
#a1
x1 <- mydataframe$a1[mydataframe$brand==1]
x2 <- mydataframe$a1[mydataframe$brand==2]
x3 <- mydataframe$a1[mydataframe$brand==3]
library(vioplot)
vioplot(x1,x2,x3,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("电池技术性能满意度得分")
#a2
x21 <- mydataframe$a2[mydataframe$brand==1]
x22 <- mydataframe$a2[mydataframe$brand==2]
x23 <- mydataframe$a2[mydataframe$brand==3]
vioplot(x21,x22,x23,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("舒适性整体表现满意度得分")
#a3
x31 <- mydataframe$a3[mydataframe$brand==1]
x32 <- mydataframe$a3[mydataframe$brand==2]
x33 <- mydataframe$a3[mydataframe$brand==3]
vioplot(x31,x32,x33,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("经济性整体满意度得分")
#a4
x41 <- mydataframe$a4[mydataframe$brand==1]
x42 <- mydataframe$a4[mydataframe$brand==2]
x43 <- mydataframe$a4[mydataframe$brand==3]
vioplot(x41,x42,x43,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("安全性表现整体满意度得分")
#a5
x51 <- mydataframe$a5[mydataframe$brand==1]
x52 <- mydataframe$a5[mydataframe$brand==2]
x53 <- mydataframe$a5[mydataframe$brand==3]
vioplot(x51,x52,x53,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("动力性表现整体满意度得分")
#a6
x61 <- mydataframe$a6[mydataframe$brand==1]
x62 <- mydataframe$a6[mydataframe$brand==2]
x63 <- mydataframe$a6[mydataframe$brand==3]
vioplot(x61,x62,x63,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("驾驶操控性表现整体满意度得分")
#a7
x71 <- mydataframe$a7[mydataframe$brand==1]
x72 <- mydataframe$a7[mydataframe$brand==2]
x73 <- mydataframe$a7[mydataframe$brand==3]
vioplot(x71,x72,x73,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("外观内饰整体表现满意度得分")
#a8
x81 <- mydataframe$a8[mydataframe$brand==1]
x82 <- mydataframe$a8[mydataframe$brand==2]
x83 <- mydataframe$a8[mydataframe$brand==3]
vioplot(x81,x82,x83,names=c("合资品牌","自主品牌","新势力品牌"),col="gray")
title("配置与质量品质整体满意度得分")
#ggplot2画图
mydata2 <- mydataframe
mydata2$brand <- factor(mydata2$brand,levels=c(1,2,3),labels=c("joint venture brand","self-owned brand","New Power brand"))
library(ggplot2)
p1 <- ggplot(mydata2,aes(x=brand,y=a1))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(x="",title="Battery technical performance satisfaction")+theme(plot.title = element_text(hjust = 0.5)) p2 <- ggplot(mydata2,aes(x=brand,y=a2))+geom_violin(fill="gray")+geom_boxplot(fill="gray",width=0.1)+labs(x="",title="Comfort overall performance satisfaction")+theme(plot.title = element_text(hjust = 0.5))
p3 <- ggplot(mydata2,aes(x=brand,y=a3))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(x="",title="Overall economic satisfaction")+theme(plot.title = element_text(hjust = 0.5))
p4 <- ggplot(mydata2,aes(x=brand,y=a4))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(x="",title="Safety represents overall satisfaction")+theme(plot.title = element_text(hjust = 0.5))
p5 <- ggplot(mydata2,aes(x=brand,y=a5))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(x="",title="Overall satisfaction with \n dynamic performance")+theme(plot.title = element_text(hjust = 0.5)) p6 <- ggplot(mydata2,aes(x=brand,y=a6))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(x="",title="Overall satisfaction with \n handling performance")+theme(plot.title = element_text(hjust = 0.5)) p7 <- ggplot(mydata2,aes(x=brand,y=a7))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(x="",title="Overall performance satisfaction score \n of exterior and interior decoration")+theme(plot.title = element_text(hjust = 0.5)) p8 <- ggplot(mydata2,aes(x=brand,y=a8))+geom_violin(fill="gray")+geom_boxplot(fill="lightgray",width=0.1)+labs(title="Configuration and quality \n overall satisfaction",x="")+theme(plot.title = element_text(hjust = 0.5))
library(gridExtra)
grid.arrange(p1,p2,p3,p4,ncol=2)
grid.arrange(p5,p6,p7,p8,ncol=2)
效果图:
这篇关于R语言画小提琴图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!