corinne r
corinne r

Reputation: 300

How can I keep all tick marks but remove most grid lines on the x axis?

Suppose I want to make a plot like the one specified below using ggplot, however I'd like to keep all the tick marks on the x axis (for each integer) but only show grid lines at 5, 10, 15, 20, and 25. How do I modify my code to remove the extraneous grid lines?

too many grid lines!

ggplot(cars, aes(x = speed, y = dist)) + 
geom_point() + 
scale_x_continuous(breaks = seq(1, 25, 1), 
                   limits = c(1, 25),
                   labels = seq(1, 25, 1)) + 
theme(panel.grid.minor.x = element_blank())

Upvotes: 7

Views: 3734

Answers (1)

eipi10
eipi10

Reputation: 93761

You can remove all the grid lines with a theme statement, but then create new grid lines using geom_vline. For example:

ggplot(cars, aes(x = speed, y = dist)) + 
  geom_vline(xintercept=seq(0,25,5), colour="white") +
  geom_point() + 
  scale_x_continuous(breaks=1:25, limits=c(1,25)) +   
  theme(panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank())

enter image description here

Upvotes: 10

Related Questions