在ggplot2中设置直方图中断[重复]

时间:2021-12-07 14:55:11

This question already has an answer here:

这个问题在这里已有答案:

I am new to ggplot2 and I am trying to obtain the same histogram that I would with hist(results, breaks = 30).

我是ggplot2的新手,我试图获得与hist相同的直方图(结果,break = 30)。

How do I replicate the same histogram with ggplot2? I am playing with the binwidth parameter of the geom_histogram, but I am having a hard time making the two histograms look identical.

如何使用ggplot2复制相同的直方图?我正在使用geom_histogram的binwidth参数,但是我很难让两个直方图看起来相同。

2 个解决方案

#1


If you use the code you will see how the R decided to break up your data:

如果您使用该代码,您将看到R决定如何分解您的数据:

data(mtcars)
histinfo <- hist(mtcars$mpg)

From the histinfo you will get the necessary information concerning the breaks.

从histinfo您将获得有关休息的必要信息。

$breaks
[1] 10 15 20 25 30 35

$counts
[1]  6 12  8  2  4

$density
[1] 0.0375 0.0750 0.0500 0.0125 0.0250

$mids
[1] 12.5 17.5 22.5 27.5 32.5

$xname
[1] "mtcars$mpg"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
> 

Now you can tweak the code below to make your ggplot histogram, look more like the base one. You would have to change axis labels, scale and colours. theme_bw() will help you to get some settings in order.

现在你可以调整下面的代码来制作你的ggplot直方图,看起来更像是基础代码。您必须更改轴标签,比例和颜色。 theme_bw()将帮助您按顺序获取一些设置。

data(mtcars)
require(ggplot2)
qplot(mtcars$mpg,
      geom="histogram", 
      binwidth = 5) +
    theme_bw()

and change the binwidth value to whatever suits you. 在ggplot2中设置直方图中断[重复]

并将binwidth值更改为适合您的任何值。

#2


Adding to @Konrad's answer, instead of using hist you can use one of the nclass.* functions directly (see the nclass documentation). There are three functions that are used by hist:

添加到@ Konrad的答案,而不是使用hist,您可以直接使用其中一个nclass。*函数(请参阅nclass文档)。 hist使用三个函数:

nclass.Sturges uses Sturges' formula, implicitly basing bin sizes on the range of the data.

nclass.Sturges使用Sturges的公式,隐含地将bin大小基于数据范围。

nclass.scott uses Scott's choice for a normal distribution based on the estimate of the standard error, unless that is zero where it returns 1.

nclass.scott根据标准误差的估计使用Scott选择正态分布,除非它返回1时为零。

nclass.FD uses the Freedman-Diaconis choice based on the inter-quartile range (IQR) unless that's zero where it reverts to mad(x, constant = 2) and when that is 0 as well, returns 1.

nclass.FD使用基于四分位数间距(IQR)的Freedman-Diaconis选项,除非它返回到mad(x,常数= 2)时为零,当它为0时返回1。

The hist function by default uses nclass.Sturges.

默认情况下,hist函数使用nclass.Sturges。

#1


If you use the code you will see how the R decided to break up your data:

如果您使用该代码,您将看到R决定如何分解您的数据:

data(mtcars)
histinfo <- hist(mtcars$mpg)

From the histinfo you will get the necessary information concerning the breaks.

从histinfo您将获得有关休息的必要信息。

$breaks
[1] 10 15 20 25 30 35

$counts
[1]  6 12  8  2  4

$density
[1] 0.0375 0.0750 0.0500 0.0125 0.0250

$mids
[1] 12.5 17.5 22.5 27.5 32.5

$xname
[1] "mtcars$mpg"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
> 

Now you can tweak the code below to make your ggplot histogram, look more like the base one. You would have to change axis labels, scale and colours. theme_bw() will help you to get some settings in order.

现在你可以调整下面的代码来制作你的ggplot直方图,看起来更像是基础代码。您必须更改轴标签,比例和颜色。 theme_bw()将帮助您按顺序获取一些设置。

data(mtcars)
require(ggplot2)
qplot(mtcars$mpg,
      geom="histogram", 
      binwidth = 5) +
    theme_bw()

and change the binwidth value to whatever suits you. 在ggplot2中设置直方图中断[重复]

并将binwidth值更改为适合您的任何值。

#2


Adding to @Konrad's answer, instead of using hist you can use one of the nclass.* functions directly (see the nclass documentation). There are three functions that are used by hist:

添加到@ Konrad的答案,而不是使用hist,您可以直接使用其中一个nclass。*函数(请参阅nclass文档)。 hist使用三个函数:

nclass.Sturges uses Sturges' formula, implicitly basing bin sizes on the range of the data.

nclass.Sturges使用Sturges的公式,隐含地将bin大小基于数据范围。

nclass.scott uses Scott's choice for a normal distribution based on the estimate of the standard error, unless that is zero where it returns 1.

nclass.scott根据标准误差的估计使用Scott选择正态分布,除非它返回1时为零。

nclass.FD uses the Freedman-Diaconis choice based on the inter-quartile range (IQR) unless that's zero where it reverts to mad(x, constant = 2) and when that is 0 as well, returns 1.

nclass.FD使用基于四分位数间距(IQR)的Freedman-Diaconis选项,除非它返回到mad(x,常数= 2)时为零,当它为0时返回1。

The hist function by default uses nclass.Sturges.

默认情况下,hist函数使用nclass.Sturges。