Search code examples
rggplot2frequency-distribution

R: Creating ggplot histograms to mirror freq() function


I'm trying to use ggplot to create a bar plot (or histogram) to mirror the freq function from the descr package (where each discrete value in the variable gets its own column in the frequency plot, with the x-ais ticks centered around each value), but I'm having some trouble getting this to work.

Here is what I'm trying to create (but using ggplot so I can use its nice graphics):

library(ggplot2)
library(descr)

variable <- c(0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 7)
df <- data.frame(variable)
freq(df$variable)

And here is my trying (and failing) to do the same in ggplot:

histo.variable <- ggplot(df, aes(x = variable)) + # create histogram
  geom_bar(stat = "bin") +
  xlab("Variable Value") +
  ylab("Count") +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
histo.variable

As you can see, the bars are not centered on the tick marks. (Additionally, it'd be great to get rid of the little half-lines in between the bars.)

Thanks to anyone who can help!


Solution

  • Maybe like this:

    ggplot(df, aes(x = variable)) + 
      geom_histogram(aes(y = ..density..),      
                     binwidth = 1,
                     colour = "blue", fill = "lightblue")
    

    freq