R語言與醫學統計圖形-【14】ggplot2幾何對象之直方密度圖


ggplot2繪圖系統——幾何對象之直方圖、密度圖

1.直方圖

參數。

geom_histogram(mapping = ,
               data = ,
               stat = 'bin', #統計變換,概率密度為density
               position = 'stack',
               binwidth = , #條柱寬度
               bins = , #條柱數目,默認30
               na.rm = FALSE,
               show.legend = NA,
               inherit.aes = TRUE
               )

示例。

ggplot(diamonds,aes(carat))+
  geom_histogram(fill='darkorchid4')

ggplot(diamonds,aes(carat))+
  geom_histogram(fill='darkorchid4',bins=45)+
  xlim(0,3)

image.png

# bins和binwidth
ggplot(diamonds,aes(carat))+
  geom_histogram(fill='darkorchid4',bins = 300)

ggplot(diamonds,aes(carat))+
  geom_histogram(fill='darkorchid4',binwidth = 0.01)
#binwidth參數是一個相對寬度

image.png

#顏色映射
ggplot(diamonds,aes(price,fill=cut))+
  geom_histogram(binwidth = 500)

image.png

2.概率密度曲線

三種方法:

#stat=density
ggplot(diamonds,aes(price))+
  geom_histogram(color='darkorchid4',stat='density')

#aes映射,..density..表示將y軸表示為density
ggplot(diamonds,aes(price, ..density..))+
  geom_histogram(fill='darkorchid4',color='black')

#geom_density
ggplot(diamonds,aes(depth,fill=cut,color=cut))+
  geom_density(alpha=0.1)+xlim(55,70)

image.png


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM