Chapter 06—Basic graphs

三. 柱状图(Histogram)

1. hist():画柱状图

·breaks(可选项):控制柱状图的小柱子的条数;

·freq=FALSE:基于概率(probability),而非频率(frequencies),绘制图形。

·还可以有其他参数,如:xlab,ylab,main,col,lwd...

2. lines():在已有图形上添加线条。

3. box():给已有图形添加一个框。

4. rug()

5. diff()

6.box()

例07:

> par(mfrow=c(2,2))
> hist(mtcars$mpg)
>
> hist(mtcars$mpg,breaks=12,col="red",xlab="Miles Per Gallon",main="Histogram,rug plot,density curve")
>

>
hist(mtcars$mpg,freq=FALSE,breaks=12,col="red",xlab="Miles Per Gallon",main="Histogram,rug plot,density curve")
> rug(jitter(mtcars$mpg))
> lines(density(mtcars$mpg),col="blue",lwd=2)

> x<-mtcars$mpg
> h<-hist(x,breaks=12,col="red",xlab="Miles Per Gallon",main="Histogram with normal curve and box")
> xfit<-seq(min(x),max(x),length=40)
> yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
> yfit<-yfit*diff(h$mids[1:2]*length(x))
> lines(xfit,yfit,col="blue",lwd=2)
> box()

四. 中心密度曲线(kernel dentity plots)

plot(dentity(x)):画中心密度曲线

sm包中的函数sm.density.compare()函数:在同一个图中,画出多组中心密度曲线。

 例08:

> par(mfrow=c(2,1))
> > d<-density(mtcars$mpg)
> plot(d)
> > d<-density(mtcars$mpg)
> plot(d,main="Kernel Density of Miles Per Gallon")
> polygon(d,col="red",border="blue")
> rug(mtcars$mpg,col="brown")

 

 

 

 例09:

> install.packages("sm")
trying URL 'http://cran.rstudio.com/bin/windows/contrib/3.0/sm_2.2-5.3.zip'
Content type 'application/zip' length 445006 bytes (434 Kb)
opened URL
downloaded 434 Kb

package ‘sm’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
	C:\Users\seven-wang\AppData\Local\Temp\RtmpYfr9xm\downloaded_packages
> library(sm)
Package `sm', version 2.2-5: type help(sm) for summary information
> par(lwd=2)
> attach(mtcars)
> cyl.f<-factor(cyl,levels=c(4,6,8),labels=c("4 cylinder","6 cylinder","8 ylinder"))
> sm.density.compare(mpg,cyl,xlab="Miles Per Gallon")
> title(main="MPG Distribution by Car Cylinders")
> colfill<-c(2:(1+length(levels(cyl.f))))
> legend(locator(1),levels(cyl.f),fill=colfill)
> detach()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 





 
posted @ 2013-08-04 01:23  seven_wang  阅读(521)  评论(0编辑  收藏  举报