R語言----繪圖學習筆記之Scatter plots


前言

  最近某項目要搞數據挖掘,需要對數據進行可視化顯示,原本我是打算直接用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)

image

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

?plot

你就清楚用法了

xyplot

數據匯總方法

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

image

格柵

qplot()

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

image

標識點

plot(mpg~disp,data=mtcars)
text(160,21,"Mazdz RX4")

 

image

 

抖動(jitter)

x <- rbinom(1000, 10, 0.25)
y <- rbinom(1000, 10, 0.25)
plot(x, y)

image

抖動后

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

 

image

x所有點都可以顯示出來

 

直線模式:

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

 

image

非線性模式的曲線:

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,我也搞不清楚這樣了解對不對)

plot(cars, main="測試lowess")
lines(lowess(cars), col="red")
lines(lowess(cars, f=0.3), col="blue")

 

image

制作3D視圖

需要使用 scattersplot3d 包

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

 

image

QQ圖(研究正態分布的一種圖…)

qqnorm(mtcars$mpg)
qqline(mtcars$mpg)

image

在坐標軸上顯示數據密度

x <- rnorm(1000)
plot(density(x))
rug(x)

image

大數據的平滑分散圖顯示

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

 

image

這么看正態分布圖挺帶感的

 

資源檢索

http://addictedtor.free.fr/graphiques/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM