Is there a reason my histogram bin spans multiple values, even though there is only one value in the dataframe?

40 Views Asked by At

I have a df that looks like:

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

All the numeric values are the same and I want to make a very simple historgram. However, when I plot it, I get something like

hist(df$value)

enter image description here

With {ggplot2} I tried:

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

I am not sure why the bin spans so many values.

I was expecting on bar representative of -0.042 with a count of 20. I have tried adjusting bins, and binwidth, but I am at a loss.

1

There are 1 best solutions below

0
Josep Pueyo On

Is this what you need? The secret is converting your value to a factor.

library(ggplot2)

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

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

Created on 2024-02-11 with reprex v2.0.2