天天看点

数据分析,展现与R语言学习笔记(2)

各种图的低级版本

对a1进行直方图分析,a1为一个向量      
> hist(a$a1)      

绘制散点图

> plot(a$a2,a$a3)      

列联表分析

> table(a$a1)


 68  71  72  74  75  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92 
  1   1   3   1   2   1   3   1   2   2   2   5   3   2   1   7   5   3   7   3   4 
 93  94  95  96  97  98  99 100 101 102 103 104 105 108 109 114 117 130 
  2   7   3   2   3   3   3   4   1   1   2   3   1   2   1   1   1   1 
> barplot(table(a$a1))      

饼图

> pie(table(a$a1))      

箱尾图(箱线图)

> boxplot(a1,a2,a3)      
> boxplot(a[2:4],col=c("red","green","blue",notch=T))      
星图      
> stars(a)      

茎叶图

> stem(a2)


  The decimal point is 2 digit(s) to the right of the |


  -0 | 6
  -0 | 221
   0 | 0122223333334444
   0 | 555555666666666777777777888888888899999
   1 | 0000111222222233334444444
   1 | 5555666778899
   2 | 013      

QQ图(检验是不是正态分布)

> qqnorm(a1)
> qqline(a1)
> qqnorm(a2)
> qqline(a2)      

各种图的高级版本:

散点图:

> plot(a1,a2,main="数学分析与线性代数成绩的关系",xlab="数学分析",ylab="线性代数",xlim=c(0,120),ylim=c(0,120),xaxs="i",yaxs="i",col="red",pch=19)      

连线图:

> a
[1] 2 3 4 5 6
> b
[1]  4  7  8  9 12
> plot(a,b,type="l")      

多条曲线的效果:

> plot(a,b,type="l",col="red",ylim=c(0,20),main="Hello World",xlab="Month",ylab="RainFall",lwd=2)
> lines(a1,type="l",col="blue",lwd=2)
> lines(a2,type="l",col="green",lwd=2)
> lines(a3,type="l",col="orange",lwd=2)      

密度图:

> plot(density(rnorm(1000)))      

热力图:

> heatmap(as.matrix(mtcars),Rowv=NA,Colv=NA,col=heat.colors(256),scale="column",margins=c(2,8),main="Car characteristic by Model")      

几个内置数据集:

data函数会显示所有的内置的数据集,所以变量命名时不要用data,会显示不出来。

> data()      
> iris        //鸢尾花数据集      
> sunflowerplot(iris[,3:4],col="gold",seg.col="gold")      

向日葵散点图

散点图集:

> pairs(iris[,1:4])      

用plot实现跟上边一样的效果

> plot(iris[,1:4],main="Relationships between characteristics of iris flowers",pch=19,col="blue",cex=0.9)      

利用par()在同一个device输出多个散点图

> par(mfrow=c(3,1))     //画出图来是3行1列
> plot(a1,a2)
> plot(a2,a3)
> plot(a1,a3)      

查看R所支持的所有颜色

> colors()      

绘图设备的控制

> dev.new()
> dev.list()
windows windows 
      2       3 
> dev.cur()
windows 
      3 
> dev.next()
windows 
      2 
> dev.cur()
windows 
      3 
> dev.prev()
windows 
      2 
> dev.cur()
windows 
      3 
> dev.set(2)
windows 
      2 
> dev.cur()
windows 
      2 
> dev.off(2)
windows 
      3 
> dev.cur()
windows 
      3 
> dev.list()
windows 
      3 
> graphics.off()
> dev.list()
NULL
> dev.new()
> dev.list()
windows 
      2 
>      

绘图参数位置控制参数的控制

看文档

三维作图

scatterplot3d包需要安装

> library("scatterplot3d")
> x=y=seq(-2*pi,2*pi,pi/15)
> f=function(x,y) sin(x)*sin(y)
> z=outer(x,y,f)
> contour(x,y,z,col="blue")
> dev.new()
> persp(x,y,z,theta=30,phi=30,expand=0.7,col="lightblue")      

调和曲线图(聚类的时候做判断,几个簇,就有几把线)

地图

安装maps包和geosphere包

> dev.new()
> library(maps)
> library(sp)
> library(geosphere)
> map("state")
> map("world")      

地图的作用举例:

社交数据可视化

> xlim=c(-171.738281,56.856229)//经纬度
> ylim=c(12.039321,71.856229)
> map("world",col="#f2f2f2",fill=T,bg="white",lwd=0.05,xlim=xlim,ylim=ylim)      
> lon_ca=-121.640625
> lat_me=-45.21300
> lat_me=-45.213004
> lon_me=-68.936250
> inter=gcIntermediate(c(lon_ca,lat_ca),c(lon_me,lat_me),n=50,addStartEnd=T)
> lines(inter)
> map("world")
> lines(inter)      
airports=read.csv("http://datasets.flowingdata.com/tuts/maparcs/airports.csv",header=T)  //直接从网上拉数据
flights=read.csv("http://datasets.flowingdata.com/tuts/maparcs/flights.csv",header=T,as.is=T)
xlim=c(-171.738281,56.856229)
ylim=c(12.039321,71.856229)
map("world",col="#f2f2f2",fill=T,bg="white",lwd=0.05)


fsub=flights[flights$airline=="AA",]  //取flight的airline字段为AA的行(a[i,]就是取a的第i行,a[,j]就是去a的第j列)


for(j in 1:length(fsub$airline)){
  air1=airports[airports$iata==fsub[j,]$airport1,]
  air2=airports[airports$iata==fsub[j,]$airport2,]
  
  inter=gcIntermediate(c(air1[1,]$long,air1[1,]$lat),c(air2[1,]$long,air2[1,]$lat),n=100,addStartEnd=T)
  
  lines(inter,col="red",lwd=0.8)
}      

继续阅读