Dan
Dan

Reputation: 12074

ggplot2: ticks not showing at minor_breaks

This may be due to a caffeine deficiency, but I can't get minor_breaks to produce minor tick marks in ggplot2. For example,

# Load data
data(mtcars)

# Load library 
library(ggplot2)

# Create plot
p <- ggplot(data = mtcars, aes(x = wt, y = mpg)) 
p <- p + geom_point()
p <- p + scale_x_continuous(minor_breaks = seq(2.5, 4.5, by =1))
print(p)

This produces the following plot:

enter image description here

Note: no minor tick marks at 2.5, 3.5, and 4.5. There are no errors or warnings, either. Now, if I simply change minor_breaks to breaks, major tick marks appear at those positions as one would expect, which makes me think that my syntax is correct and just that the minor tick marks are invisible for some reason. Point out my error and be richly rewarded with e-high-fives.

Upvotes: 3

Views: 3132

Answers (1)

PatrickT
PatrickT

Reputation: 10510

The image you provide was not produced by the code you post. You seem to be using a theme: perhaps you deleted the line of code applying the theme or you have set a theme by default somewhere and you've forgotten about it. Either way, some themes override the default effect of minor_breaks. Illustration:

Your code:

data(mtcars)
library(ggplot2)
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
scale_x_continuous(minor_breaks = seq(2.5, 4.5, by =1))

produces this:

enter image description here

To see the connection between minor_breaks and the grid lines, change the values to something like: scale_x_continuous(minor_breaks = seq(2.3, 4.3, by =1)):

enter image description here

I believe you are using the classic theme: If you add +theme_classic() to the previous code, you get:

enter image description here

showing that the gridlines set by minor_breaks are overridden and disappear.

Upvotes: 1

Related Questions