我认为这可能部分是R问题,部分是统计问题,因此,如果有更好的地方,请原谅(如果是,请告诉我在哪里)。

假设我有一个像这样的数据集my_measurements:

> glimpse(my_measurements)
Observations: 200
Variables: 2
$ sample_id <int> 18, 22, 30, 59, 74, 126, 133, 137, 147, 186, 189, 195, 203, 248, 294, 303, 320, 324, 353, 3...
$ value     <dbl> 0.9565217, 1.0000000, 0.7500000, 0.7142857, 1.0000000, 0.8571429, 1.0000000, 1.0000000, 0.8...

其中每个sample_id具有对某物的相应度量,从而使value在0和1之间(例如,它们可能是某物的比例)。

完整的dput()输出为:
structure(list(sample_id = c(18L, 22L, 30L, 59L, 74L, 126L, 133L,
137L, 147L, 186L, 189L, 195L, 203L, 248L, 294L, 303L, 320L, 324L,
353L, 375L, 384L, 385L, 395L, 400L, 401L, 411L, 459L, 468L, 479L,
482L, 497L, 502L, 528L, 556L, 576L, 601L, 640L, 657L, 659L, 674L,
687L, 688L, 709L, 711L, 716L, 737L, 744L, 771L, 784L, 791L, 793L,
794L, 813L, 845L, 854L, 864L, 866L, 887L, 891L, 899L, 915L, 917L,
919L, 934L, 948L, 969L, 975L, 980L, 998L, 1006L, 1011L, 1015L,
1021L, 1036L, 1047L, 1056L, 1062L, 1073L, 1074L, 1082L, 1087L,
1101L, 1102L, 1108L, 1113L, 1119L, 1130L, 1160L, 1175L, 1176L,
1179L, 1187L, 1188L, 1206L, 1224L, 1227L, 1411L, 1412L, 1431L,
1472L, 1481L, 1485L, 1488L, 1491L, 1501L, 1519L, 1531L, 1534L,
1537L, 1559L, 1579L, 1592L, 1603L, 1608L, 1629L, 1643L, 1684L,
1721L, 1726L, 1736L, 1744L, 1756L, 1778L, 1800L, 1807L, 1813L,
1829L, 1839L, 1901L, 1905L, 1926L, 1975L, 1980L, 2004L, 2006L,
2019L, 2062L, 2069L, 2079L, 2087L, 2091L, 2116L, 2123L, 2141L,
2147L, 2159L, 2160L, 2163L, 2168L, 2173L, 2191L, 2194L, 2208L,
2214L, 2231L, 2244L, 2246L, 2253L, 2273L, 2290L, 2291L, 2302L,
2318L, 2326L, 2353L, 2371L, 2372L, 2388L, 2412L, 2415L, 2423L,
2443L, 2451L, 2452L, 2468L, 2470L, 2472L, 2481L, 2485L, 2502L,
2503L, 2504L, 2521L, 2572L, 2601L, 2621L, 2625L, 2635L, 2643L,
2644L, 2674L, 2698L, 2710L, 2723L, 2742L, 2757L, 2794L, 2824L,
2835L, 2837L), value = c(0.956521739130435, 1, 0.75, 0.714285714285714,
1, 0.857142857142857, 1, 1, 0.869565217391304, 0, 0.892857142857143,
0.9, 1, 0.892857142857143, 1, 1, 0, 0.883333333333333, 1, 0.976190476190476,
0.973684210526316, 0.914285714285714, 1, 0.6, 0.6, 1, 0.931818181818182,
1, 0.882352941176471, 0.75, 1, 1, 1, 0.826086956521739, 1, 0.8,
0.75, 1, 0.931034482758621, 1, 1, 0.980769230769231, 1, 0.875,
1, 0.985294117647059, 1, 1, 0.5, 0.826086956521739, 0.833333333333333,
0.75, 0.631578947368421, 1, 0.875, 1, 1, 0.904761904761905, 1,
1, 0.666666666666667, 0.96551724137931, 1, 0.636363636363636,
1, 0.681818181818182, 0.78125, 0.285714285714286, 0.833333333333333,
0.928571428571429, 0.991735537190083, 1, 0.5, 0.833333333333333,
0.666666666666667, 0.8, 0.666666666666667, 0.710526315789474,
0.787878787878788, 1, 1, 0.888888888888889, 1, 1, 0.703703703703704,
1, 1, 0.875, 0.686274509803922, 0.714285714285714, 1, 1, 1, 1,
1, 1, 0.805309734513274, 0.774193548387097, 1, 1, 1, 0.62962962962963,
1, 0.782608695652174, 1, 1, 0.5, 0.666666666666667, 1, 1, 0.5,
0.5, 0.555555555555556, 0.666666666666667, 0.5, 0.5, 0.697674418604651,
0.593220338983051, 1, 0.6, 1, 1, 0.615384615384615, 0.673913043478261,
0.5, 1, 1, 0, 1, 1, 0.555555555555556, 0.366666666666667, 0.333333333333333,
1, 1, 1, 0.888888888888889, 1, 1, 1, 1, 1, 1, 0.6, 0.26530612244898,
1, 0.3, 1, 1, 0.5, 1, 1, 1, 0.888888888888889, 0.666666666666667,
1, 1, 0.866666666666667, 0.193548387096774, 1, 1, 0.181818181818182,
1, 1, 0.947368421052632, 1, 1, 1, 0.851851851851852, 1, 1, 0.0769230769230769,
0.125, 0.1875, 1, 0.230769230769231, 0.111111111111111, 1, 1,
0.444444444444444, 1, 0.5, 0.153846153846154, 0.3, 0, 0.0714285714285714,
0.166666666666667, 1, 0.166666666666667, 1, 0.181818181818182,
0.0714285714285714, 0.142857142857143, 1, 0, 0, 0.888888888888889,
0, 0, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-200L))

我能够使用ggplot()geom_histogram()绘制values分布的直方图,这向我展示了其中很多接近1的情况:
ggplot(data = my_measurements) +
    geom_histogram(mapping = aes(x = value))

[

然后,我尝试使用geom_density()绘制相同的数据:
library(ggplot2)
ggplot(data = my_measurements) +
    geom_density(mapping = aes(x = value))

r - geom_density y轴大于1-LMLPHP

我感到困惑的是,为什么y轴(“密度”)大于1?我(可能是错误的)理解此曲线下的总面积应为1。如果不是,(a)如何解释该图,以及(b)如果我希望曲线下的面积为1,如何我会做吗?

最佳答案

您必须在..scaled..中使用geom_density(密度估计,最大缩放为1),默认情况下,它使用..density..

library(ggplot2)
# In aes by default first argument is x and second argument is y
ggplot(my_measurements, aes(value, ..scaled..)) +
    geom_density()

r - geom_density y轴大于1-LMLPHP

重现结果的所有代码:
library(ggplot2)
p1 <- ggplot(my_measurements, aes(value, ..density..)) +
    geom_density() +
    ggtitle("Density")
p2 <- ggplot(my_measurements, aes(value, ..count..)) +
    geom_density() +
    ggtitle("Count")
p3 <- ggplot(my_measurements, aes(value, ..scaled..)) +
    geom_density() +
    ggtitle("Scaled")
egg::ggarrange(p1, p2, p3, ncol = 3)

关于r - geom_density y轴大于1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51385455/

10-12 18:55