服务热线
178 0020 3020
R语言之ggplot2画饼图/箱型图
任务1:
将数据表中中文名改成对应的英文名("Number","Code","Value")
library(ggplot2)
data<-read.csv("D:/Study/R/R2/R.4-1.csv",header=T)
print(data)
data=data[order(data$Value,decreasing = T),]
mylabel=as.vector(data$Code)
mylabel=paste(mylabel,"(",round(data$Value/sum(data$Value)*100,2),"%)",sep = "")
P=ggplot(data,aes(x="",y=Value,fill=Code))+
geom_bar(stat = "identity",width = 1)+
coord_polar(theta = "y")+
labs(x="city",y="value",title="R2-03-7")+
theme(axis.ticks = element_blank())+
theme(legend.title = element_blank(),legend.position = "top")+
scale_fill_discrete(breaks=data$Code,labels=mylabel)+
theme(axis.text.x = element_blank())
ggsave("E:/PNG/R2/R2-4-饼图、箱形图/R2-03-7.png",width=4,height=4)
library(ggplot2)
data<-read.csv("D:/Study/R/R2/R.4-1.csv",header=T)
print(data)
data=data[order(data$Value,decreasing = TRUE),]
mylabel=as.vector(data$Code)
mylabel=paste(mylabel,"(",round(data$Value/sum(data$Value)*100,2),"%)",sep = "")
P=ggplot(data,aes(x="",y=Value,fill=Code))+
geom_bar(stat = "identity",width = 0.2)+
coord_polar(theta = "y")+
labs(x="",y="",title="R2-03-8")+
theme(axis.ticks = element_blank())+
theme(legend.position = "none")+
scale_fill_discrete(breaks=data$Code,labels=mylabel)+
theme(axis.text.x = element_blank())+
theme(panel.grid=element_blank()) +
theme(panel.border=element_blank())
ggsave("E:/PNG/R2/R2-4-饼图、箱形图/R2-03-8.png",width=4,height=4)
任务2:
library("lattice")
str(singer)
P=ggplot(data = singer,aes(x = voice.part,y = height,fill = voice.part))+
geom_boxplot()+
theme(legend.position = 'top',legend.title=element_blank())+
labs(title = "R2-03-9")
ggsave("E:/PNG/R2/R2-4-饼图、箱形图/R2-03-9.png",width=6,height=4)
附件