11

Say I have the following data:

require(ggplot2)
set.seed(123)
data <- data.frame(x = sample(1:20, 100, replace = TRUE))

I want to create a dot plot of data$x, so this is what I do:

ggplot(data, aes(x)) +
  geom_dotplot(binwidth = 1) +
  scale_x_continuous(breaks = seq(1, 20, 1)) +
  scale_y_continuous(breaks = NULL)

Which gives me this:

dotplot1

I would like to get rid of those vertical grid lines, so I add theme(line = element_blank()) to my ggplot statement. The problem is that command also eliminates the tick marks, which I would like to keep. How can I hide the grid lines whilst keeping their respective ticks?

I would also like to know how I can change the grid lines so they'll be drawn every 1:20, not at every 0.5 mark.

I've looked for those answers in ?title() and ?geom_dotplot, tried a couple of things, to no avail. Appreciate the help!

Waldir Leoncio
  • 10,853
  • 19
  • 77
  • 107

2 Answers2

21

Use panel.grid

theme(panel.grid = element_blank())

enter image description here

If you want the grid lines to be drawn every 1:20, not at every 0.5 mark.

 theme(panel.grid.minor =   element_blank(),
        panel.grid.major =   element_line(colour = "white",size=0.75))

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
3

you might find answers here

opts has been changed to theme. but the thing about grid.major.x, grid.minor.y should set on the track

To get rid of the major grid line, for example

ggplot(data, aes(x)) +
    geom_dotplot(binwidth = 1) +
    scale_x_continuous(breaks = seq(1, 20, 1)) +
    scale_y_continuous(breaks = NULL) + theme(panel.grid.major = element_blank())
Community
  • 1
  • 1
Anto
  • 1,189
  • 10
  • 16