一:直方图
1.bin的含义
直方图中bin的含义:计算颜色直方图需要将颜色空间划分为若干小的颜色区间,即直方图的bin,通过计算颜色在每个小区间内德像素得到颜色直方图,bin越多,直方图对颜色的分辨率越强,但增加了计算机的负担。即(上图所分10个竖条区域,每个竖条区域称为一个bin)
ggplot(data=mpg,aes(x=displ,fill=fl)) + geom_histogram(binwidth=0.2,position=”stack”)
#binwidth 为The width of the bins;不同于条形图的width
#position是指位置调整,stack是堆叠即同组几何对象堆叠
![](https://img.haomeiwen.com/i14744215/93345d85a32ee96c.png)
ggplot(data=mpg,aes(x=displ,fill=fl)) + geom_histogram(binwidth=0.4,position='dodge')
#position是指位置调整,dodge是同组几何对象并列
位置调整的参数还有:fill jitter identity
![](https://img.haomeiwen.com/i14744215/a8d91b4d33c0c3fd.png)
二:箱线图
ggplot(data=mpg,aes(x=factor(fl),y=hwy)) + geom_boxplot(fill = 'red', color='grey')
##factor()因子化
![](https://img.haomeiwen.com/i14744215/e1b5cda9a3c9829a.png)
ggplot(data=mpg,aes(x=1,y=hwy)) + geom_boxplot(fill='grey',color='blue',outlier.colour= "yellow", outlier.shape = 1) ###高亮异常值并赋予特定的几何对象
![](https://img.haomeiwen.com/i14744215/2c987495034e73af.png)
三:曲线密度图
ggplot(data=mpg,aes(x=displ,fill=class)) + geom_density(color='blue',size=0.1,alpha=I(0.3))
![](https://img.haomeiwen.com/i14744215/ff478bc71f8b6ec7.png)
ggplot(data=mpg,aes(x=displ,y=..density..)) + geom_histogram(fill='red',binwidth=0.18,alpha=I(0.3)) + geom_density(color='blue',size=0.5)
几乎看不到密度曲线。原因:直方图和密度图结合在一起。直方图中bin变换生成y变量有count和density,默认使用前者,这样由于count很大,density很小(总是小于1),就会值得密度线处于低位,难以看到,所以需要y=..density..(引用bin变换的数据必须前后加双圆点)
![](https://img.haomeiwen.com/i14744215/8b09fef0ca39273e.png)
ggplot(data=mpg,aes(x=displ))+geom_histogram(fill='red',binwidth=0.18,alpha=I(0.3))+geom_density(color='blue',size=0.8)
![](https://img.haomeiwen.com/i14744215/d286801c754e8f78.png)
网友评论