1. Histogram
# Simple Histogram
hist (mtcars$mpg) #未指定任何选项时的默认图形
# colored histogram with specified number of bins
hist (mtcars$mpg,
breaks=12, #分组个数
col= "Red", #红色
xlab= "Miles per gallon", # x axis label
main= "colored histogram with Bins") # title
# Colored histogram with rug plot, frame, and specified number of bins
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)
# histogram with superimposed normal curve (to Peter Dalgaard)
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 ()
2. Nuclear density map
D <-Density (mtcars$mpg) # returns density data
D <-Density (mtcars$mpg)
plot (d, main= "Kernel density of Miles per gallon")
Polygon (d, col= "Red", border= " Blue ")
Reference: "R in Action–data analysis and Graphics with R"