分享

绘图专题 | 条形图/直方图傻傻分不清楚

 生物_医药_科研 2019-01-26

相信很多人都会把条形图、直方图、柱状图混着叫,难以说出其中区别。

在ggplot2中,其实只有两个函数 geom_bar()geom_histogram(),分别对应了条形图(也有人喜欢叫柱状图...),以及直方图。

所以,这两个函数的区别在哪?

使用 ggplot2 包中提供的 diamonds 数据集作为测试数据:

https://ggplot2./reference/diamonds.html

A dataset containing the prices and other attributes of almost 54,000 diamonds.

  1. p_load(ggplot2)

  2. data(diamonds)

绘制前10个钻石的价格分布:

  1. TestData = diamonds[1:10,]    # 使用前10个数据

条形图(Barplot)

在 ggplot2 中图层函数 geom_bar() 可以绘制条形图:

https://ggplot2./reference/geom_bar.html

geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use geom_col() instead.

  1. ggplot(TestData, aes(x = price)) + geom_bar()

该图中横坐标为价格,纵坐标为每个价格对应的钻石数,所以最终钻石数总计为10!

此时如果我们绘制所有的 diamonds 数据:

  1. nrow(diamonds)    # 53940

  2. ggplot(diamonds, aes(x = price)) + geom_bar()

可见横坐标price分布过于密集,因为每个价格都被绘制,因为:

geom_bar() uses stat_count() by default: it counts the number of cases at each x position.

查看 geom_bar() 函数源码:

  1. function (mapping = NULL, data = NULL, stat = 'count', position = 'stack',

  2.    ..., width = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA,

  3.    inherit.aes = TRUE)

可知,默认 stat='count' ,即 geom_bar() 默认对横坐标的每个点(价格)统计数目!

但是,如果想将价格分割/区域化,例如统计每100价格区间对应的钻石数目,可以设定 binwidth 参数:

  1. ggplot(diamonds, aes(x = price)) + geom_bar(binwidth = 100)

正常得出结果:

但有如下警告

Warning message:

geom_bar() no longer has a binwidth parameter. Please use geom_histogram() instead.

geom_bar() 函数将不再支持 binwidth 参数。虽然当前还能使用,但建议使用 geom_histogram() 绘制这种对数据进行分割/区域的图形!

直方图(Histogram)

https://ggplot2./reference/geom_histogram.html

geom_histogram() for continuous data.

Visualise the distribution of a single continuous variable by dividing the x axis into binsand counting the number of observations in each bin.

geom_histogram() is an alias for geom_bar() plus stat_bin()

stat_bin(), which bins data in ranges and counts the cases in each range. It differs from stat_count(), which counts the number of cases at each x position (without binning into ranges). stat_bin() requires continuous x data, whereas stat_count() can be used for both discrete and continuous x data.

可见,与 geom_bar() 中默认使用的 stat_count() 对单个点的计数不同, geom_histogram() 默认使用 stat_bin() 将数据基于一定的范围分割,并分别统计每个范围(bin)内的数目。

查看 geom_histogram 源码可知,默认使用 stat='bin' ,且默认分为30个bins:

  1. ggplot(diamonds, aes(x = price)) + geom_histogram()

stat_bin() using bins=30. Pick better value with binwidth

同样,可以将范围设置为100:

  1. ggplot(diamonds, aes(x = price)) + geom_histogram(binwidth = 100)

此时,得到的图形结果将与前文中 geom_bar(binwidth=100) 的结果一致!

而如果在 geom_histogram()  中设置 stat='count'则绘图效果等同于 geom_bar()

  1. ggplot(diamonds, aes(x = price)) + geom_histogram(stat = 'count')

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多