Not that essential to be honest, but something that can improve the readability of a plot: the axis ticks. Again, ggplot does quite an good job at setting up the scale and thus the ticks along the X- and Y-axes. But you can still have a different opinion about where these ticks have to be, or how many should be there…
Let’s start with the code for a reference plot. This plot is stored in the object baseplot
so that we can reuse it throughout the whole tutorial:
baseplot <- ggplot(df, aes(values1, values2)) +
geom_point(size=2)
baseplot
Axis ticks on the Y-axis may be defined by the argument breaks=
in scale_y_continuous()
(note that it is of course possible to do the same on the X-axis using scale_x_continuous()
). The syntax is breaks=c(a, b, ...)
where a, b, ...
are the ticks that you want to set on the axis. You can put as many as you want, not just 2. Here for example, we want to put 12, 30 and 55 (and only these values) on the Y-axis:
baseplot +
scale_y_continuous(breaks=c(12, 30, 55))
To define the ticks in a more regular/organized manner, you may use break=seq(a, b, c)
where a
is the start of the interval where the ticks are displayed, b
is the end of the interval where the ticks are displayed, and c
defines the frequency of the ticks in the given interval. In the following example, we will start the ticks at 0 and stop at 50 on both the X-axis and Y-axis, and the ticks will be placed every 10:
baseplot +
scale_x_continuous(breaks=seq(0, 50, 10)) +
scale_y_continuous(breaks=seq(0, 50, 10))
It is also possible to make things a bit more complex, for example by setting two or more intervals, each with specific ticks. In the example below, we set on the X-axis a series of ticks every 5 between 0 and 50, and every 10 between 50 and 90. On the Y-axis, we set a series of ticks every 5 between 0 and 50, and every 10 between 50 and 90:
baseplot +
scale_x_continuous(breaks=c(seq(0, 50, 5), seq(50, 90, 10))) +
scale_y_continuous(breaks=c(seq(0, 50, 5), seq(50, 90, 10)))
Here, you may notice that the X- and Y-axes stops before 90. ggplot knows that all values (data points) are well below 90, and thus resets automatically the upper limit of the axes. If you wish to impose the axis limits while setting the ticks, use the arguments limits = c(a, b)
to set the lower limit to a
and the upper limit to b
(use NA
to let ggplot compute the best lower/upper limit):
baseplot +
scale_x_continuous(limits=c(NA,90), breaks=c(seq(0, 50, 5), seq(50, 90, 10))) +
scale_y_continuous(limits=c(NA,90), breaks=c(seq(0, 60, 5), seq(60, 90, 10)))
If one or both of the axes have a logarithmic scale (see here), then it is also possible to set ticks in another way than the one imposed by default by ggplot. Use annotation_logticks()
to display the ticks, then use sides=
to define which side of the plot (i.e. which axis) will be affected by the modifications. Write l
for left, r
for right, b
for bottom and t
for top. Combinations such as lr
or lb
are possible.
baseplot +
scale_y_continuous(trans = "log10") +
annotation_logticks(sides="l")