R语言----绘图学习笔记之Scatter plots

本文转载自: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

帮助用法:

命令行里面直接打

?你要查的命令即可

基础用法:

1
plot(cars$dist~cars$speed)

image

更多用法在R控制台中打上

1
?plot

你就清楚用法了

xyplot

数据汇总方法

1
xyplot(Sepal.Length~Sepal.Width,data=iris,groups=Species,auto.key=list(corner=c( 1 , 1 )))

image

格栅

qplot()

1
qplot(Sepal.Length,Sepal.Width,data=iris,col=as.factor(Species),size=as.factor(Species),shape=as.factor(Species))

image

标识点
1
2
plot(mpg~disp,data=mtcars)
text( 160 , 21 , "Mazdz RX4" )

 

image

 

抖动(jitter)
1
2
3
x <- rbinom( 1000 , 10 , 0.25 )
y <- rbinom( 1000 , 10 , 0.25 )
plot(x, y)

image

抖动后

1
plot(jitter(x),jitter(y))

 

image

x所有点都可以显示出来

 

直线模式:

1
2
3
plot(mtcars$mpg~mtcars$disp)
lmfit <- lm(mtcars$mpg~mtcars$disp)
abline(lmfit)

 

image

非线性模式的曲线:

1
2
3
4
5
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" )

 

image

非参数值的曲线(英文是non-parametric,我也搞不清楚这样了解对不对)

1
2
3
plot(cars, main= "测试lowess" )
lines(lowess(cars), col= "red" )
lines(lowess(cars, f= 0.3 ), col= "blue" )

 

image

制作3D视图

需要使用 scattersplot3d 包

1
scatterplot(mtcars$wt, mtcars$disp, mtcars$mpg)

 

image

QQ图(研究正态分布的一种图…)

1
2
qqnorm(mtcars$mpg)
qqline(mtcars$mpg)

image

在坐标轴上显示数据密度

1
2
3
x <- rnorm( 1000 )
plot(density(x))
rug(x)

image

大数据的平滑分散图显示

1
2
3
4
n <- 1000000
x <- matrix(rnorm(n), ncol= 2 )
y <- matrix(rnorm(n,mean= 3 ,sd= 1.5 ), ncol= 2 )
smoothScatter(x,y)

 

image

这么看正态分布图挺带感的


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值