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?
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())
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.
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.
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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With