0

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!

RSS
  • 163
  • 1
  • 2
  • 11

1 Answers1

0

Maybe like this:

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

freq

AK88
  • 2,946
  • 2
  • 12
  • 31
  • That's really nice; thank you! Ideally I'd have the count on the y-axis, but doesn't matter too much if it's density. Thanks again. – RSS Jun 09 '17 at 12:25
  • You can sub `..density..` to `..count..` in that case. – AK88 Jun 09 '17 at 12:28