各種圖的低級版本
對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)
}