gpt4 book ai didi

r - ggplot2 在使用自定义 x 轴限制绘制直方图时丢失数据

转载 作者:行者123 更新时间:2023-12-03 09:07:23 25 4
gpt4 key购买 nike

我试图用 these data 绘制六个直方图(2 列数据(卡路里、钠)x 3 种类型(牛肉、肉类、家禽))我想给它们相同的 x 轴和 y 轴比例。我使用 scale_x_continuous 来限制 x 轴,根据各种来源,它会删除不会出现在绘图上的数据。这是我的代码:

#src.table is the data frame containing my data
histogram <- function(df, dataset, n_bins, label) {
ggplot(df, aes(x=df[[dataset]])) +
geom_histogram(color="darkblue", fill="lightblue", bins = n_bins) + xlab(label)
}
src2_12.beef <- src2_12.table[src2_12.table$Type == "Beef",]
src2_12.meat <- src2_12.table[src2_12.table$Type == "Meat",]
src2_12.poultry <- src2_12.table[src2_12.table$Type == "Poultry",]

src2_12.calories_scale <- lims(x = c(min(src2_12.table$Calories), max(src2_12.table$Calories)), y = c(0, 6))
src2_12.sodium_scale <- lims(x = c(min(src2_12.table$Sodium), max(src2_12.table$Sodium)), y = c(0, 6))
#src2_12.calories_scale <- lims()
#src2_12.sodium_scale <- lims()

src2_12.plots <- list(
histogram(src2_12.beef, "Calories", 10, "Calories-Beef") + src2_12.calories_scale,
histogram(src2_12.meat, "Calories", 10, "Calories-Meat") + src2_12.calories_scale,
histogram(src2_12.poultry, "Calories", 10, "Calories-Poultry") + src2_12.calories_scale,
histogram(src2_12.beef, "Sodium", 10, "Sodium-Beef") + src2_12.sodium_scale,
histogram(src2_12.meat, "Sodium", 10, "Sodium-Meat") + src2_12.sodium_scale,
histogram(src2_12.poultry, "Sodium", 10, "Sodium-Poultry") + src2_12.sodium_scale
)
multiplot(plotlist = src2_12.plots, cols = 2, layout = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE))

这是输出: output

对比数据应该是什么样子: enter image description here

我无法理解为什么缺少一些数据点,因为我设置的限制已经是数据的最小值和最大值。

最佳答案

您可能想使用 coord_cartesian 而不是 lims。当您摆弄直方图的限制时,可能会发生意想不到的事情,因为从原始数据到实际直方图必须进行相当多的繁琐转换。

让我们深入了解一个例子:

p <- ggplot(src2_12.beef,aes(x = Calories)) + 
geom_histogram(bins = 10)
p1 <- ggplot(src2_12.beef,aes(x = Calories)) +
geom_histogram(bins = 10) +
lims(x = c(86,195))

a <- ggplot_build(p)
b <- ggplot_build(p1)

>a$data[[1]][,1:5]
y count x xmin xmax
1 1 1 114.1111 109.7222 118.5000
2 0 0 122.8889 118.5000 127.2778
3 3 3 131.6667 127.2778 136.0556
4 2 2 140.4444 136.0556 144.8333
5 5 5 149.2222 144.8333 153.6111
6 2 2 158.0000 153.6111 162.3889
7 0 0 166.7778 162.3889 171.1667
8 2 2 175.5556 171.1667 179.9444
9 3 3 184.3333 179.9444 188.7222
10 2 2 193.1111 188.7222 197.5000

> b$data[[1]][,1:5]
y count x xmin xmax
1 0 0 NA NA 90.83333
2 0 0 96.88889 90.83333 102.94444
3 1 1 109.00000 102.94444 115.05556
4 0 0 121.11111 115.05556 127.16667
5 4 4 133.22222 127.16667 139.27778
6 4 4 145.33333 139.27778 151.38889
7 4 4 157.44444 151.38889 163.50000
8 1 1 169.55556 163.50000 175.61111
9 4 4 181.66667 175.61111 187.72222
10 2 2 193.77778 187.72222 NA
>

所以现在您想知道,这到底是怎么发生的,对吧?

好吧,当您告诉 ggplot 您需要 10 个 bin 并且 x 限制从 86 到 195 时,直方图算法会尝试创建跨越该实际范围的 10 个 bin。这就是为什么它尝试创建低于 100 个的 bin,即使那里没有数据。

然后可能会发生更多奇怪的情况,因为条形可能会超出标称数据范围(xminxmax 值),因为条形宽度通常会略高于和略低于实际数据的高端和低端。

coord_cartesian 将在所有这些处理发生后调整 x 限制,因此它会绕过所有这些小怪癖。

关于r - ggplot2 在使用自定义 x 轴限制绘制直方图时丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46105803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com