即使数据框中只有一个值,我的直方图箱跨越多个值是否有原因?

问题描述 投票:0回答:1

我有一个看起来像这样的 df:

df = data.frame(behavior = rep("proximity", 20),
                value = rep(-0.04280145, 20))

所有数值都相同,我想制作一个非常简单的直方图。然而,当我绘制它时,我得到类似的东西

hist(df$value)

{ggplot2}
我尝试过:

library(ggplot2)
ggplot(df) + 
 geom_histogram(aes(x = value)

我不确定为什么 bin 跨越这么多值。

我期待着

-0.042
的酒吧代表,计数为
20
。我尝试过调整 bin 和 binwidth,但我不知所措。

r ggplot2 plot histogram geom-histogram
1个回答
0
投票

这是您需要的吗?秘诀是将你的价值转换为一个因素。

library(ggplot2)

df <- data.frame(behavior = rep("proximity", 20),
                value = rep(-0.04280145, 20))

df |> 
  ggplot(aes(factor(value))) +
  geom_bar()

创建于 2024-02-11,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.