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)

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

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

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)

