Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot2绘制具有百分比的多个数据集的直方图_R_Ggplot2_Histogram_Percentage - Fatal编程技术网

R 使用ggplot2绘制具有百分比的多个数据集的直方图

R 使用ggplot2绘制具有百分比的多个数据集的直方图,r,ggplot2,histogram,percentage,R,Ggplot2,Histogram,Percentage,我有四个数据集,我想在同一个图上绘制所有数据的直方图。我把所有的数据放在一个数据框中。我还可以在一个图上绘制直方图。然而,我在绘制百分比而不是计数时遇到了困难。当我使用下面的代码时,它将百分比绘制为所有计数的总和,但我更希望百分比是相对于每个数据集的。这可能吗 all <- rbind(data.frame(fill = "A", Events = A$Events), data.frame(fill = "B", Events = B$Events), data.fr

我有四个数据集,我想在同一个图上绘制所有数据的直方图。我把所有的数据放在一个数据框中。我还可以在一个图上绘制直方图。然而,我在绘制百分比而不是计数时遇到了困难。当我使用下面的代码时,它将百分比绘制为所有计数的总和,但我更希望百分比是相对于每个数据集的。这可能吗

all <- rbind(data.frame(fill = "A", Events = A$Events), 
    data.frame(fill = "B", Events = B$Events), 
    data.frame(fill = "C", Events = C$Events), 
    data.frame(fill = "D", Events = D$Events)
ggplot(all,aes(x=Events, fill = fill)) + 
 geom_histogram(aes(y = ..count../sum(..count..)), position = 'dodge')

您很接近,但需要使用
(…密度..)*binwidth
而不是
。count../sum(…count..)

旧答案

以下是一个例子:

library(ggplot2)

ggplot(mtcars,aes(x=mpg, fill = as.factor(cyl))) +
  geom_histogram(aes(y = ..density..), position = 'dodge', binwidth=5)
作为检查,将binwidth调整为100,每个列的值为0.01(100%/100=0.01)

编辑)下面是另一个示例,使用过于简化的数据集突出显示结果:

library(data.table)
# Calculate the average miles per gallon by number of cylinders
mtcars_avg <- as.data.table(mtcars)[,
                                    list(mpg_avg=mean(mpg)),
                                    by=list(cyl=as.factor(cyl))][order(cyl)][order(cyl)]
mtcars_avg
#   cyl  mpg_avg
#1:   4 26.66364
#2:   6 19.74286
#3:   8 15.10000

# OP version, with unwanted results of 33% per color (cyl)
ggplot(mtcars_avg, aes(x=mpg_avg, fill=cyl)) +
  geom_histogram(aes(y = ..count../sum(..count..)), position = 'dodge', binwidth=1)

你也可以考虑使用<代码> GeoMeStime代替:

ggplot(mtcars,aes(x=mpg, fill = as.factor(cyl))) + geom_density(alpha=0.5)

这个binwidth是必需的,因为根据定义,整数和为1。 基本上,箱宽的
x
增加导致箱宽的
y
变化
1/x
-箱宽更大,因此必须降低高度才能获得相同的面积

因此,要恢复百分比,您必须通过将
y
乘以
bw
来纠正此错误

Aa一个简单的例子,想象一下一点:

  • 基本的“
    .density..
    ”代码将为您提供百分比,因为
    bw*p=1
    ,即
    1*1=1
  • 如果将
    bw
    更改为2,则“
    .density..
    ”代码将在y轴上显示:
    bw*y=1=>y=1/bw=0.5
  • 为了获得
    y
    轴上的百分比,必须乘以
    bw

也许我没有正确回答我的问题。我想象的是,每个数据集的所有条相加等于100%,但使用密度,我得到所有数据集的总数为100%。是的,我想我理解。我在解决方案中添加了另一个示例。这有用吗?也许你可以发布一些真实的数据来突出你的关注点?我已经在上面添加了一些示例数据。使用..密度。。如果我保留binwidth=1,效果很好,但例如,当我更改binwidth=10时,总数最终为10%(我猜是因为1.00/10=.1)。如果您知道一种改变binwidth的方法,但将比例保持在100%,这将非常有用。另外,感谢您提供有关几何密度的提示。我可以用它来代替。看起来将密度乘以binwidth将获得所需的结果。检查编辑后的答案。也许其他人可以解释为什么这是必要的。请正确格式化您的答案。就目前而言,这是相当难以理解的。
library(ggplot2)

ggplot(mtcars,aes(x=mpg, fill = as.factor(cyl))) +
  geom_histogram(aes(y = ..density..), position = 'dodge', binwidth=5)
library(data.table)
# Calculate the average miles per gallon by number of cylinders
mtcars_avg <- as.data.table(mtcars)[,
                                    list(mpg_avg=mean(mpg)),
                                    by=list(cyl=as.factor(cyl))][order(cyl)][order(cyl)]
mtcars_avg
#   cyl  mpg_avg
#1:   4 26.66364
#2:   6 19.74286
#3:   8 15.10000

# OP version, with unwanted results of 33% per color (cyl)
ggplot(mtcars_avg, aes(x=mpg_avg, fill=cyl)) +
  geom_histogram(aes(y = ..count../sum(..count..)), position = 'dodge', binwidth=1)
# ..density.. version, which shows the desired results of 100% per color (cyl)
ggplot(mtcars_avg, aes(x=mpg_avg, fill=cyl)) +
  geom_histogram(aes(y = ..density..), position = 'dodge', binwidth=1)
ggplot(mtcars,aes(x=mpg, fill = as.factor(cyl))) + geom_density(alpha=0.5)