I'm generating plots for some data, but the number of ticks is too small, I need more precision on the reading.
Is there some way to increase the number of axis ticks in ggplot2?
I know I can tell ggplot to use a vector as axis ticks, but what I want is to increase the number of ticks, for all data. In other words, I want the tick number to be calculated from the data.
Possibly ggplot do this internally with some algorithm, but I couldn't find how it does it, to change according to what I want.
You can override ggplots default scales by modifying scale_x_continuous
and/or scale_y_continuous
. For example:
library(ggplot2)
dat <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(dat, aes(x,y)) +
geom_point()
Gives you this:
https://i.stack.imgur.com/d5jFT.jpg
And overriding the scales can give you something like this:
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(breaks = round(seq(min(dat$x), max(dat$x), by = 0.5),1)) +
scale_y_continuous(breaks = round(seq(min(dat$y), max(dat$y), by = 0.5),1))
https://i.stack.imgur.com/brQ5z.jpg
If you want to simply "zoom" in on a specific part of a plot, look at xlim()
and ylim()
respectively. Good insight can also be found here to understand the other arguments as well.
Based on Daniel Krizian's comment, you can also use the pretty_breaks
function from the scales
library, which is imported automatically:
ggplot(dat, aes(x,y)) + geom_point() +
scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10))
All you have to do is insert the number of ticks wanted for n
.
A slightly less useful solution (since you have to specify the data variable again), you can use the built-in pretty
function:
ggplot(dat, aes(x,y)) + geom_point() +
scale_x_continuous(breaks = pretty(dat$x, n = 10)) +
scale_y_continuous(breaks = pretty(dat$y, n = 10))
scales
but doesn't add the functions to your namespace. You can therefore call them without the import as scales::pretty_breaks(n = 10)
.
scales::pretty_breaks
handles these perfectly! I can't beleive that it has taken me so long to find it.
n.breaks
parameter provides the same functionality, but is really compact. Both great answers!
You can supply a function argument to scale
, and ggplot will use that function to calculate the tick locations.
library(ggplot2)
dat <- data.frame(x = rnorm(100), y = rnorm(100))
number_ticks <- function(n) {function(limits) pretty(limits, n)}
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(breaks=number_ticks(10)) +
scale_y_continuous(breaks=number_ticks(10))
number_ticks
. This has already been implemented in pretty_breaks {scales}
. Hence: ggplot(dat, aes(x,y)) + geom_point() + scale_x_continuous(breaks=pretty_breaks(n=10)) + scale_y_continuous(breaks=pretty_breaks(n=10))
require(scales)
2) this seems to prevent my breaks appearing in scientific notation, hence 1e6 is changed to 1000000 ??
pretty
without the scales
package, just provide the values as an argument. For example: (breaks=pretty(dat$x, n=10))
Starting from v3.3.0, ggplot2
has an option n.breaks
to automatically generate breaks for scale_x_continuous
and scale_y_continuous
library(ggplot2)
plt <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point()
plt +
scale_x_continuous(n.breaks = 5)
https://i.stack.imgur.com/RUxXg.png
plt +
scale_x_continuous(n.breaks = 10) +
scale_y_continuous(n.breaks = 10)
https://i.stack.imgur.com/pyTrw.png
scales::pretty_breaks
, but is more compact and clear. Thanks!
breaks = seq(1:10, 1)
or n.breaks = 10
(assuming I have a min of 1 and max of 10), I would like to specify something like break.increments = 1
. So if there are 12 data points there would be 12 ticks / labels. ggplot
already has the data, so it seems like it can figure out the min/max.
Additionally,
ggplot(dat, aes(x,y)) +
geom_point() +
scale_x_continuous(breaks = seq(min(dat$x), max(dat$x), by = 0.05))
Works for binned or discrete scaled x-axis data (I.e., rounding not necessary).
A reply to this question and How set labels on the X and Y axises by equal intervals in R ggplot?
mtcars %>%
ggplot(aes(mpg, disp)) +
geom_point() +
geom_smooth() +
scale_y_continuous(limits = c(0, 500),
breaks = seq(0,500,50)) +
scale_x_continuous(limits = c(0,40),
breaks = seq(0,40,5))
Success story sharing
by
argument, to different scales of numbers, i.e., 0.5 is a good value for this data which range is c(-3,3), but it's not a good range for a data which range is c(0,5000). Is there some function that calculates it?max-min/30
is a pretty common "bucket" size...but that may or may not be a good starting point for you.scale_x_date(date_breaks = "5 months", date_minor_breaks = "1 months")
coord_cartesian
should be used instead ofxlim
. stackoverflow.com/questions/25685185/…