前言
最近某項目要搞數據挖掘,需要對數據進行可視化顯示,原本我是打算直接用excel 算了,打算,用了一段時間,發現有些數據圖用excel麻煩得要命,然后,上網找了一下,原來,有在這方面也有一門專門的語言----R語言,我發現,用它繪制數據圖十分強大,就打算花幾天,就學習如何用R語言繪制數據圖
散布圖(scatter plots)
需要掌握的命令:
plot()
xyplot()
qplot()
text()
smoothScatter()
matrix()
jitter()
rbinom()
rnorm()
lines()
lowess()
nls()
用的的包:
ggplot2
lattice
scattersplot3d
幫助用法:
命令行里面直接打
?你要查的命令即可
基礎用法:
plot(cars$dist~cars$speed)更多用法在R控制台中打上
?plot你就清楚用法了
xyplot
數據匯總方法
xyplot(Sepal.Length~Sepal.Width,data=iris,groups=Species,auto.key=list(corner=c(1,1)))格柵
qplot()
qplot(Sepal.Length,Sepal.Width,data=iris,col=as.factor(Species),size=as.factor(Species),shape=as.factor(Species))標識點
plot(mpg~disp,data=mtcars) text(160,21,"Mazdz RX4")
抖動(jitter)
x <- rbinom(1000, 10, 0.25) y <- rbinom(1000, 10, 0.25) plot(x, y)抖動后
plot(jitter(x),jitter(y))
x所有點都可以顯示出來
直線模式:
plot(mtcars$mpg~mtcars$disp) lmfit <- lm(mtcars$mpg~mtcars$disp) abline(lmfit)
非線性模式的曲線:
x <- -(1:100)/10 y <- 100+10*exp(x/2)+rnorm(x)/10 nlmod <- nls(y~Const+A*exp(B*x),trace=TRUE) plot(x,y) lines(x,predict(nlmod),col="red")
非參數值的曲線(英文是non-parametric,我也搞不清楚這樣了解對不對)
plot(cars, main="測試lowess") lines(lowess(cars), col="red") lines(lowess(cars, f=0.3), col="blue")
制作3D視圖
需要使用 scattersplot3d 包
scatterplot(mtcars$wt, mtcars$disp, mtcars$mpg)
QQ圖(研究正態分布的一種圖…)
qqnorm(mtcars$mpg) qqline(mtcars$mpg)在坐標軸上顯示數據密度
x <- rnorm(1000) plot(density(x)) rug(x)大數據的平滑分散圖顯示
n <- 1000000 x <- matrix(rnorm(n), ncol=2) y <- matrix(rnorm(n,mean=3,sd=1.5), ncol=2) smoothScatter(x,y)
這么看正態分布圖挺帶感的
資源檢索