You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ggplot2叠加直方图的密度曲线匹配问题求助

ggplot2叠加直方图的密度曲线匹配问题求助

我刚接触编程和R,现在想用ggplot2做一组叠加直方图,同时配上对应的密度曲线。我在论坛上搜了不少类似帖子,但因为自己基础太弱,很难照着别人的方案改出适合自己的结果。

最开始我遇到的问题是密度曲线太小,后来摸索出一段代码,勉强做出了看起来“成功”的图(之所以打引号是因为我不确定曲线是不是真的和直方图匹配),对应的代码和数据集如下:

df <- data.frame(Method = c(rep("A", 20), rep("B", 20), rep("C", 20)), 
                 Rating = c(2, 2, 3.5, 2.5, 2.5, 3, 3.5, 3.5, 3.5, 3.5, 3.5, 3.5, 4, 3, 3, 4, 2.5, 2.5, 3, 2.5, 3, 3, 2, 2.5, 2, 2, 3, 2.5, 2.5, 3, 3, 2, 3, 3, 3, 2, 2, 2, 2.5, 3, 3, 3, 3, 3, 3.5, 3, 3, 1.5, 3.5, 4, 3.5, 3.5, 3.5, 2.5, 1.5, 2.5, 3, 2.5, 2, 2))

ggplot(df, aes(x = Rating, fill = Method, color = Method)) + 
  geom_histogram(aes(y = ..count..), position = position_dodge(width = 0.2), alpha = 0.4, binwidth = 0.5, bins = 8, closed = "right") + 
  geom_density(aes(y = ..density.. * 0.5 * after_stat(count)), size = 1, fill = NA) + 
  scale_x_continuous(breaks = seq(1, 5, by = 0.5), expand = c(0, 0)) + 
  scale_y_continuous(limits = c(0, NA), breaks = scales::breaks_width(1),expand = c(0, 0)) + 
  coord_cartesian(xlim = c(1, 5)) + 
  theme_minimal(base_size = 14) + 
  labs(x = "Rating", y = "Frequency") + 
  theme(axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"), axis.line.x.top = element_blank(), axis.line.y.right = element_blank())

但当我用同样的参数处理另一个实验的数据集时,密度曲线变得特别大,完全和直方图不匹配,对应的代码和数据集是:

df <- data.frame(Method = c(rep("A", 20), rep("B", 20), rep("C", 20)), 
                 Rating = c(1.5, 1.5, 3, 1.5, 1.5, 2.5, 2, 3, 3, 3, 3, 2.5, 2, 2.5, 3, 3, 2.5, 2.5, 2, 2, 2.5, 2.5, 2, 2.5, 1.5, 2, 2.5, 2.5, 2.5, 2.5, 2.5, 2, 2.5, 2.5, 2, 2, 2.5, 2, 2, 2.5, 2, 3, 2.5, 2.5, 3, 3, 3, 1.5, 2.5, 3, 3.5, 3, 3, 2.5, 2, 2.5, 3.5, 2.5, 2.5, 2))

ggplot(df, aes(x = Rating, fill = Method, color = Method)) + 
  geom_histogram(aes(y = after_stat(count)), position = position_dodge(width = 0.2), alpha = 0.4, binwidth = 0.5, bins = 8, closed = "right") + 
  geom_density(aes(y = ..density.. * 0.5 * after_stat(count)), size = 1, fill = NA, position = "identity") + 
  scale_x_continuous(breaks = seq(1, 5, by = 0.5), expand = c(0, 0)) + 
  scale_y_continuous(limits = c(0, NA), breaks = scales::breaks_width(1),expand = c(0, 0)) + 
  coord_cartesian(xlim = c(1, 5)) + 
  theme_minimal(base_size = 14) + 
  labs(x = "Rating", y = "Frequency") + 
  theme(axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"), axis.line.x.top = element_blank(), axis.line.y.right = element_blank())

我实在搞不懂怎么调整才能让曲线和直方图匹配上。另外附上我最开始没调整geom_histogram时的代码,当时的问题就是曲线和直方图完全对不上:

ggplot(df, aes(x = Rating, fill = Method, color = Method)) + 
  geom_histogram(aes(y = after_stat(count)), position = position_dodge(width = 0.2), alpha = 0.4, binwidth = 0.5, bins = 8, closed = "right") + 
  geom_density(size = 1, fill = NA, position = "identity") + 
  scale_x_continuous(breaks = seq(1, 5, by = 0.5), expand = c(0, 0)) + 
  scale_y_continuous(limits = c(0, NA), breaks = scales::breaks_width(1),expand = c(0, 0)) + 
  coord_cartesian(xlim = c(1, 5)) + 
  theme_minimal(base_size = 14) + 
  labs(x = "Rating", y = "Frequency") + 
  theme(axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"), axis.line.x.top = element_blank(), axis.line.y.right = element_blank())

我试着修改geom_histogram的参数,期望能让密度曲线和直方图匹配,但都没成功,希望能得到大家的帮助!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.07 08:03:05