each<-read.csv("C:\\Users\\Administrator\\Desktop\\R\\数据三\\eachcl.csv")
cl<-colMeans(each)
mycolorscl <- c(heat.colors(11)[1:11])
barplot(cl[sort.list(cl,decreasing=T)[10:1]],names.arg=clname[sort.list(cl,decreasing=T)[10:1]],xlab="city",ylab="value",
cl<-colMeans(each)
clname<-variable.names(each)
#取变量名
cl<-as.numeric(cl)mycolorscl <- c(heat.colors(11)[1:11])
barplot(cl[sort.list(cl,decreasing=T)[10:1]],names.arg=clname[sort.list(cl,decreasing=T)[10:1]],xlab="city",ylab="value",
main="空气污染指数",col=mycolorscl[10:1],border=mycolorscl[10:1],ylim = c(0,70))
#sort.list取排序位置
叠图:(ggpl0t)
#叠图:
allend<-read.csv("C:\\Users\\Administrator\\Desktop\\R\\数据三\\allend.csv")
allggplot<-allend[2:4]
for (i in 1:9) {
test<-allend[(3*i+2):(3*i+4)]
colnames(test)<-c("how","quanty","city")
allggplot<-rbind(allggplot,test)
}
p1=ggplot(allggplot,aes(x=city,fill=how))+geom_bar(position="stack")+labs(title="各城市空气质量",y="天数")+theme(plot.title = element_text(size=20,face = "bold",hjust = 0.7))
p1
本文展示了如何使用R语言进行数据处理和可视化,通过读取CSV文件并进行数据整合,利用ggplot2库创建了一个展示各城市空气质量的柱状叠图。图表以城市为X轴,空气质量指数为Y轴,用不同颜色区分不同污染级别,并进行了排序,以清晰地显示各城市的污染天数。
247

被折叠的 条评论
为什么被折叠?



