Connor Mooneyhan
Connor Mooneyhan

Reputation: 557

How can I change the grid line spacing on a ggplot2 dotplot?

I'm analyzing data from the result of pulling 10 numbered balls from a jar with replacement, repeated 70 times. Here's my code (data included):

numbers <- c(8, 3, 9, 5, 1, 9, 10, 8, 8, 1, 9, 9, 8, 5, 1, 10, 5, 9, 6, 4, 10, 3,
             10, 9, 8, 4, 8, 8, 9, 9, 1, 5, 9, 8, 4, 1, 8, 6, 7, 8, 2, 9, 5, 6,
             10, 9, 1, 1, 5, 6, 2, 8, 6, 5, 2, 5, 4, 10, 10, 2, 2, 4, 9, 6, 9,
             9, 6, 10, 9, 10)

num_frame <- data.frame(numbers)

ggplot(num_frame) +
  geom_dotplot(aes(numbers), binwidth = 1, dotsize = 0.4) +
  theme_bw() +
  xlab("Numbers") +
  ylab("Frequency")

The resulting plot is nice, except it labels gridlines at 0, 2.5, 5, 7.5, and 10, which is obviously not what I want. The scale is fine, but I would like the gridlines to be at integer values 1 through 10 (0 is fine too if necessary). How can I do this? I'd also like the y-axis to adjust likewise so that the grid is still square. Thanks!

Upvotes: 2

Views: 1628

Answers (1)

Ricardo Semi&#227;o
Ricardo Semi&#227;o

Reputation: 4456

Just add:

scale_x_continuous(breaks=1:10, minor_breaks=NULL)

minor_breaks=NULL suppress lines that aren't at the breaks

Upvotes: 2

Related Questions