Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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())
like image 799
corinne r Avatar asked Aug 25 '17 23:08

corinne r


People also ask

How do you set the x-axis ticks?

xticks( ticks ) sets the x-axis tick values, which are the locations along the x-axis where the tick marks appear. Specify ticks as a vector of increasing values; for example, [0 2 4 6] . This command affects the current axes. xt = xticks returns the current x-axis tick values as a vector.

How do I remove x-axis ticks in Matlab?

Direct link to this answerTickLength = [0 0]; This will allow you to keep the labels but remove the tick marks on only the x-axis.

How do you hide Xticks?

By using the method xticks() and yticks() you can disable the ticks and tick labels from both the x-axis and y-axis. In the above example, we use plt. xticks([]) method to invisible both the ticks and labels on the x-axis and set the ticks empty.


1 Answers

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

like image 144
eipi10 Avatar answered Nov 09 '22 09:11

eipi10