R-數據可視化-數值變量

準備工作

加載數據集

load("E:/Statistics_with_R/example/ch2/example2_2.RData")

直方圖

> par(mfrow=c(2,2),mai=c(0.6,0.6,0.4,0.1), cex=0.7)
> hist(example2_2$銷售額,xlab="銷售額",ylab="頻數",main="(a)普通")
> hist(example2_2$銷售額,breaks=20, col='lightblue', xlab="銷售額",ylab="頻數",main="(b)分成20組")
> hist(example2_2$銷售額, prob=TRUE, breaks=20,xlab="銷售額", ylab="密度",col="lightblue", main="(c)增加軸虛線和核密度線")
> rug(example2_2$銷售額)
> lines(density(example2_2$銷售額),col="red")
> hist(example2_2$銷售額, prob=TRUE, breaks=20, xlab="銷售額", ylab="密度", main="(d)增加正態密度線", col="lightblue")
> curve(dnorm(x, mean(example2_2$銷售額), sd(example2_2$銷售額)), add=T, col="red")
> rug(jitter(example2_2$銷售額))

效果圖:

 

莖葉圖

stem(example2_2$銷售額)

 

箱線圖

> par(mfrow=c(3,1), mai=c(0.4,0.2,0.3,0.2))
> x<-rnorm(1000, 50, 5)
> boxplot(x, range=1.5,col="red", horizontal=TRUE, main="相鄰值與盒子連線的箱線圖(range=1.5)", cex=0.8)
> boxplot(x, range=3,col="green", horizontal=TRUE, main="相鄰值與盒子連線的箱線圖(range=3)", cex=0.8)
> boxplot(x, range=0, varwidth=T,col="pink", horizontal=TRUE, main="相鄰值與盒子連線的箱線圖(range=0, varwidth=T)", cex=0.8)

效果圖:

小提琴圖

load("E:/Statistics_with_R/example/ch2/example2_3.RData")
library(viplot)
x1<-example2_3$亞歷山大.彼得裏夫利
x2<-example2_3$拉爾夫.許曼
x3<-example2_3$克里斯蒂安.賴茨
x4<-example2_3$列昂尼德.葉基莫夫
x5<-example2_3$基思.桑德森
x6<-example2_3$羅曼.邦達魯克
vioplot(x1, x2,x3,x4,x5,x6,col='lightblue',names=c("亞歷山大.彼得裏夫利","拉爾夫.許曼","克里斯蒂安.賴茨","列昂尼德.葉基莫夫","基思.桑德森","羅曼.邦達魯克"))

效果如下:

點圖

load("E:/Statistics_with_R/example/ch2/example2_3.RData")
example2_3<-cbind(example2_3,id=factor(1:20))
library(reshape)
example2_3_1<-melt(example2_3,id.vars=c("id"), variable_name="運動員")
example2_3_1<-rename(example2_3_1,c(value="射擊環數"))
save(example2_3_1, file="E:/Statistics_with_R/exer/example_2_3_1.RData")
head(exmple2_3_1);tail(example2_3_1)
dotchart(example_2_3_1$射擊環數, groups=example2_3_1$運動員, xlab="射擊次數", pch=20)
library(lattice)
dotplot(射擊環數~運動員, data=example2_3_1, col="blue", pch=19)

核密度圖

library(lattice)
par(cex=0.7, mai=c(0.6, 0.6, 0.1, 0.1))
densityplot(~射擊環數|運動員, data=example2_3_1,col="blue", cex=0.5)

attach(example2_3_1)
library(sm)
sm.density.compare(射擊環數, 運動員, lty=1:6, col=1:6, lwd=1.5)
legend("topleft", legend=levels(運動員), lty=1:6, vol=1:6)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章