Histogram and kernel density diagram in R to draw _r

Source: Internet
Author: User
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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.