R语言实战

> 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")
> 

> par(lwd=2)
> library(sm)
> attach(mtcars)
> 
> cyl.f <- factor(cyl, levels=c(4,6,8), 
+                 labels=c("4 cylinder", "6 cylinder", "8 cylinder"))
> 
> 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(mtcars)
> 

原文地址:https://www.cnblogs.com/wnzhong/p/7589158.html