首页 > 解决方案 > 直方图未显示正确的计数/值?(直方图 vs Geom Freqpoly)

问题描述

我有 2002 年纽约马拉松赛和每个人的地点的数据集。我也有每个人的性别。

当我绘制一个按性别分组的直方图时,女性的计数关闭了!

当我绘制 FreqPoly 图时,分布与基于数据的预期一致。

谁能解释这种差异?红色条用于女性,蓝色条用于男性。相同的颜色适用于 freq_poly 图。

红线是女性赛车手的计数应该在哪里,但直方图显示它们的值要高得多。为什么? 在此处输入图像描述

标签: rggplot2histogram

解决方案


不是答案,而是 Ian Campbell 和 teunbrand 的答案中讨论的不同职位选项的可视化


library(ggplot2)
set.seed(1)
p1 <- ggplot()+
  geom_histogram(data = data.frame(x = rnorm(100), g = rep(1:2, 50)), aes(x, fill = factor(g)), position = "dodge")+
  ggtitle("position = dodge")

set.seed(1)
p2 <- ggplot()+
  geom_histogram(data = data.frame(x = rnorm(100), g = rep(1:2, 50)), aes(x, fill = factor(g)), position = "identity")+
  ggtitle("position = identity")

set.seed(1)
p3 <- ggplot()+
  geom_histogram(data = data.frame(x = rnorm(100), g = rep(1:2, 50)), aes(x, fill = factor(g)))+
  ggtitle("position = stack")


library(patchwork)

p1/p2/p3
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex 包于 2020-07-11 创建(v0.3.0)


推荐阅读