前     言

当我们提到直方图时,一定会想到直方图和密度图,一般这两种方法都会结合在一起,有着千丝万缕的联系,下面我们就来了解一下其中的含义以及绘图技巧!

统计直方图

统计直方图( Histogram),形状类似柱形图却有着与柱形图完全不同的含义。统计直方图涉及统计学的概念,首先要从数据中找出它的最大值和最小值,然后确定一个区间,使其包含全部测量数据,将区间分成若干小区间,统计测量结果出现在各小区间的频数M,以测量数据为横坐标,以频数M为纵坐标,划出各小区间及其对应的频数。在平面直角坐标系中,横轴标出每个组的端点,纵轴表示频数,每个矩形的高代表对应的频数,我们也称这样的统计直方图为频数分布直方图。

所以统计直方图的主要作用如下所示:

(1)能够显示各组频数或数量分布的情况;

(2)易于显示各组之间频数或数量的差别。通过统计直方图还可以观察和估计哪些数据比较集中,异常或者孤立的数据分布在何处。

统计直方图的基本参数如下所示。

(1)组数:在统计数据时,我们把数据按照不同的范围分成几个组,组的个数称为组数;

(2)组距:每一组两个端点的差;

(3)频数:分组内的数据元的数量除以组距。

核密度估计图

核密度估计图(kernel density plot )用于显示数据在X轴连续数据段内的分布状况。这种图表是直方图的变种,使用平滑曲线来绘制水平数值,从而得出更平滑的分布。核密度估计图比直方图优 胜的地方,在于它们不受所使用分组数量的影响,所以能更好地界定分布形状。核密度估计(kernel density estimation)是在概率论中用来估计未知的密度函数,属于非参数检验方法之一,由Rosenblatt( 1955 )和 Emanuel Parzen( 1962)/3提出,又名 Parzen 窗( Parzen window) 所谓核密度估计,就是采用平滑的峰值函数(核)来拟合观察到的数据点,从而对真实的概率分布曲线进行模拟。

基本参数

参数主要就是未来美化图形颜色、宽度等,如下:

1.binwidth:直方图组宽

2.bins:直方图组数

3.colour:边缘颜色

4.fill:填充颜色

5.xlab(), ylab() : x名称,y名称

6.xlim(), ylim() : x轴,y轴范围

7.ggtitle:图形标题

软件包安装

基础R语言中hist()函数可以实现绘制直方图,但是ggplot2更加流行,所以就讲讲这个吧!

if (!require(ggplot2)) install.packages("ggplot2")

library(ggplot2)

数据读取

我们这次仍然使用iris数据:

This famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica. iris is a data frame with 150 cases (rows) and 5 variables (columns) named Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.

data("iris")
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

绘制直方图

1. 绘制频数分布直方图

简单直方图绘制就是使用geom_histogram()即可实现直方图的绘制,如下:

ggplot(iris, aes(x = Petal.Length)) + geom_histogram(bin = 50) + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_数据挖掘

2. 绘制频率分布直方图

我们发现简单直方图给出来几个warning,建议我们使用bins=30,按照这个修改成30,看下结果:

Warning: Ignoring unknown parameters: bin stat_bin() using bins = 30. Pick better value with binwidth.

ggplot(iris, aes(x = Petal.Length, y = ..density..)) + geom_histogram(bins = 30) +
    theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_开发语言_02

3. 添加密度曲线

在直方图上添加密度曲线,这种方式在SCI绘图中也是非常常见的,经过密度线的添加可以突出数据的峰值,如下:

ggplot(iris, aes(x = Petal.Length, y = ..density..)) + geom_histogram(bins = 30) +
    geom_density(size = 1) + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言用plot怎么让横坐标显示日期_03

4. 美化直方图

美化其实就是通过对边框色,填充色以及透明度进行设置,如下:

ggplot(iris, aes(x = Petal.Length)) + geom_histogram(bins = 30, colour = "black",
    fill = "blue", alpha = 0.5) + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_数据挖掘_04

5. 使用默认颜色

关于颜色,我们可以设置多种颜色版,比如彩虹色。

ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30) +
    theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_人工智能_05

6. 去掉图例

这时我们看到图例显得有些多余,因为这个不属于分组问题,所以需要我们去掉图例,通过show.legend = FALSE去掉图例。

p1 <- ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 100))) + geom_histogram(bins = 30,
    show.legend = FALSE) + scale_fill_discrete() + theme_bw()
p1

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言_06

7. 使用灰度调色板

灰度图在一些特殊期刊上是十分常用的,所以特意也给大家做了一个模板,画起来没难度,只要使用scale_fill_grey()函数即可,如下:

p2 <- ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30,
    show.legend = FALSE) + scale_fill_grey() + theme_bw()
p2

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_人工智能_07

8. 使用RColorBrewer调色板

渐变色RColorBrewer调色板对应的函数就是scale_fill_brewer(),绘制直方图之后,如下:

library(RColorBrewer)
ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30,
    show.legend = FALSE) + scale_fill_brewer(palette = "Oranges") + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言_08

9. 换背景主题

有时候觉得背景色过于单调,可以调整其背景色的主题,比如我们将背景色改成橙色的,使用scale_fill_brewer()函数,如下:

p3 <- ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30,
    show.legend = FALSE) + scale_fill_brewer(palette = "Oranges") + theme_dark()
p3

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言用plot怎么让横坐标显示日期_09

10. 添加标题

添加直方图表格需要我们单独使用ggtitle这个函数,另外同样可以通过xlab(),ylab() 设计一下x,y轴的名称,如下:

ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30) +
    scale_fill_discrete() + ggtitle("Density of Petal Length") + xlab("Petal Length") +
    ylab("Density") + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言用plot怎么让横坐标显示日期_10

11. 标题居中

从上图发现正标题完全在左边,这时需要我们对标题的位置进行修正,如下:

ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30) +
    scale_fill_discrete() + ggtitle("Density of Petal Length") + xlab("Petal Length") +
    ylab("Density") + theme_bw() + theme(plot.title = element_text(hjust = 0.5))

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_人工智能_11

12. 设置x,y轴的范围

考虑到对称和美观的问题,需要重新设计一下x,y轴的范围,通过ylim(),xlim(),如下:

ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30) +
    scale_fill_discrete() + ggtitle("Density of Petal Length") + xlab("Petal Length") +
    ylab("Density") + theme(plot.title = element_text(hjust = 0.5)) + ylim(0, 30) +
    theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_人工智能_12

13. 设置x,y轴的间隔和范围

举例过大时就需要手动切配置间隔和范围,如下:

ggplot(iris, aes(x = Petal.Length, fill = cut(Petal.Length, 10))) + geom_histogram(bins = 30) +
    scale_fill_discrete() + ggtitle("Density of Petal Length") + xlab("Petal Length") +
    ylab("Density") + theme(plot.title = element_text(hjust = 0.5)) + scale_y_continuous(limits = c(0,
    30), breaks = c(0, 5, 10, 15, 20, 25, 30)) + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_开发语言_13

14. 分组直方图

按照Species进行分组,并绘制分组直方图只需要fill=Species,如下:

p4 <- ggplot(iris, aes(x = Petal.Length, fill = Species)) + geom_histogram(bins = 30) +
    theme_bw()
p4

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言用plot怎么让横坐标显示日期_14

15. 美化分组直方图

美化分组直方图只要注重颜色,柱子大小以及透明图即可:

ggplot(iris, aes(x = Petal.Length, y = ..density.., fill = Species)) + geom_histogram(bins = 30,
    alpha = 0.5, colour = "black", size = 0.025) + theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言_15

16. 添加分组密度线

添加分组密度图几乎跟简短直方图一致,不同的是这需要数据的y=..density..,及完成作图。

ggplot(iris, aes(x = Petal.Length, y = ..density.., fill = Species)) + geom_histogram(bins = 30,
    alpha = 0.5, colour = "black", size = 0.025) + geom_density(size = 0.2, alpha = 0.5) +
    theme_bw()

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_数据挖掘_16

17. 添加标题

添加分组直方图,如下:

p5 <- ggplot(iris, aes(x = Petal.Length, y = ..density.., fill = Species)) + geom_histogram(bins = 30,
    alpha = 0.5, colour = "black", size = 0.025) + geom_density(size = 0.2, alpha = 0.5) +
    ggtitle("Density of Petal Length") + xlab("Petal Length") + ylab("Density") +
    theme_bw() + theme(plot.title = element_text(hjust = 0.5))
p5

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言_17

18. 核密度估计图

我们看看去掉直方图之后只保留密度图的效果,如下:

p6 <- ggplot(iris, aes(x = Petal.Length, y = ..density.., fill = Species)) + geom_density(size = 0.2,
    alpha = 0.5) + ggtitle("Density of Petal Length") + xlab("Petal Length") + ylab("Density") +
    theme_bw() + theme(plot.title = element_text(hjust = 0.5))
p6

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_r语言_18

19. 直方图组图

最后我们将宣传几张图进行组图,炫图就出来了,是不是其实很简单呢?

library(patchwork)
(p1 | p2 | p3)/(p4 | p5 | p6)

r语言用plot怎么让横坐标显示日期 r语言boxplot横坐标_数据挖掘_19

这期直方图其实蛮简单的,在是使用过程中根据自己的数据情况进行调整参数,我相信通过我这套教程,各位老师、同学都能够实现ggplot2绘图自由,未来也会成为一名会作图的科研人员!