手把手: 如何用R作Polar图等
用R作如下的各国Gini系数的Polar barChart:
作上图的R代码为:library(ggplot2)GiniData<- read.csv(‘IncomeInequality.csv’,head=T)
Gini<- ggplot(GiniData, aes(x=paste(GiniIndex,Country),y=GiniIndex,fill=GiniIndex%/%10))Gini<- Gini +geom_bar(stat=”identity”,position=”dodge”)+coord_polar()Gini<- Gina + scale_fill_continuous(high=”darkred”,low=”darkgreen”)Gini<- Gini + theme( panel.background=element_rect(fill=”white”,colour = “white”,size=0), axis.text=element_blank(), axis.title=element_blank(), legend.title=element_blank())x <-c(1:dim(GiniData)[1])Gini +geom_text( aes( x=x, label=paste(GiniData$GiniIndex,GiniData$Country), angle=270-x/134*360, hjust=1), #hjust节制标签的对齐方法 y=GiniData$GiniIndex+3, size=3, vjust=0)有关颜色变革的代码,用黄色的底纹标示了出来。还可以试一下angle=90-x/134*360,hjust=0。这样的话,文字的偏向会反过来。作图的相关数据和功效,可以参看http://www.rpubs.com/helengyy/135140
把中国的Gini系数标出来的Polarbar Chart:
作上图的R代码为:Gini<- ggplot(GiniData, aes(x=paste(GiniIndex,Country),y=GiniIndex, fill=(sign(GiniIndex-41.60)+sign(Country==”Coted’lvoire”)*2)*sign(Country!=”China”)))Gini<- Gini + geom_bar(stat=”identity”,position=”dodge”)+coord_polar()Gini<- Gini + scale_fill_continuous(high=”darkred”,low=”darkgreen”)Gini<- Gini + theme( panel.background=element_rect(fill=”white”,colour = “white”, size=0), axis.text=element_blank(), axis.title=element_blank(), legend.title=element_blank())x <-c(1:dim(GiniData)[1])Gini +geom_text( aes( x=x, label=paste(GiniData$GiniIndex,GiniData$Country), angle=270-x/134*360, hjust=1), #hjust节制标签的对齐方法 y=GiniData$GiniIndex+3, size=3, vjust=0)代码中和第1张图纷歧样的处所,也用黄色底纹标示出来了。
以较大值最小值为顶的箱图:假设,我们有A、B、C、D、E班级的某一门科目标较高分和较低分,尚有整个年级的平均分,我们用箱图来调查各班较高分较低分距年级平均分的间隔。R代码如下:x <- matrix(c(36,97,33,89,45,99,51,93,47,88),2,5)boxplot(x,medlty=”blank”, #medlty=”blank”就是把四分位盒式图(箱图)的须须去掉names=c(“A”,”B”,”C”,”D”,”E”),col=”pink”, boxwex=0.35)abline(h=71,col=”navy”, lwd=2, lty=5)
暗示数据在较大最小之间位置的线型图:若某位同学A, B, C, D, E五门课的后果X <- matrix(c(36,88,97,33,86,89,45,77,99,51,90,93,47,65,88),3,5)Y <-c(1:5)plot(c(X[1,],X[3,],X[2,]),c(Y,Y,Y),pch = c(rep(19,10),rep(4,5)),cex = 1.5,col = c(rep(“seagreen”,10),rep(“magenta”,5)),lwd = 2,xlab = “后果”,ylab= “科目”,yaxt = “n”) #y轴的坐标标签去掉#配置y轴的坐标标签axis(2,at = c(1:5), labels = c(“A”,”B”,”C”,”D”,”E”))arrows(c(X[2,],X[2,]),c(Y,Y),c(X[1,],X[3,]), c(Y,Y),col = “springgreen”,lwd = 2,length = 0.15,angle = 20,)
从图中可以看出该学生的后果离较低分近照旧接近较高分。祝各人2016年新年新气象!
作者:顾运筠。应用数学硕士,职业院校的统计老师。对呆板进修和数据可视化感乐趣。