本文转载自:http://www.cnblogs.com/youxilua/archive/2012/01/12/2320455.html
前言
最近某项目要搞数据挖掘,需要对数据进行可视化显示,原本我是打算直接用excel 算了,打算,用了一段时间,发现有些数据图用excel麻烦得要命,然后,上网找了一下,原来,有在这方面也有一门专门的语言----R语言,我发现,用它绘制数据图十分强大,就打算花几天,就学习如何用R语言绘制数据图
散布图(scatter plots)
需要掌握的命令:
plot()
xyplot()
qplot()
text()
smoothScatter()
matrix()
jitter()
rbinom()
rnorm()
lines()
lowess()
nls()
用的的包:
ggplot2
lattice
scattersplot3d
帮助用法:
命令行里面直接打
?你要查的命令即可
基础用法:
1plot(cars$dist~cars$speed)
更多用法在R控制台中打上
1?plot
你就清楚用法了
xyplot
数据汇总方法
1xyplot(Sepal.Length~Sepal.Width,data=iris,groups=Species,auto.key=list(corner=c(
1
,
1
)))
格栅
qplot()
1qplot(Sepal.Length,Sepal.Width,data=iris,col=as.factor(Species),size=as.factor(Species),shape=as.factor(Species))
标识点
12plot(mpg~disp,data=mtcars)
text(
160
,
21
,
"Mazdz RX4"
)
抖动(jitter)
123x <- rbinom(
1000
,
10
,
0.25
)
y <- rbinom(
1000
,
10
,
0.25
)
plot(x, y)
抖动后
1plot(jitter(x),jitter(y))
x所有点都可以显示出来
直线模式:
123plot(mtcars$mpg~mtcars$disp)
lmfit <- lm(mtcars$mpg~mtcars$disp)
abline(lmfit)
非线性模式的曲线:
12345x <- -(
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,我也搞不清楚这样了解对不对)
123plot(cars, main=
"测试lowess"
)
lines(lowess(cars), col=
"red"
)
lines(lowess(cars, f=
0.3
), col=
"blue"
)
制作3D视图
需要使用 scattersplot3d 包
1scatterplot(mtcars$wt, mtcars$disp, mtcars$mpg)
QQ图(研究正态分布的一种图…)
12qqnorm(mtcars$mpg)
qqline(mtcars$mpg)
在坐标轴上显示数据密度
123x <- rnorm(
1000
)
plot(density(x))
rug(x)
大数据的平滑分散图显示
1234n <-
1000000
x <- matrix(rnorm(n), ncol=
2
)
y <- matrix(rnorm(n,mean=
3
,sd=
1.5
), ncol=
2
)
smoothScatter(x,y)
这么看正态分布图挺带感的