Axis Ticks
Sometimes you may want to change the way your tick marks are displayed. You can change the spacing, the size, and the force whatever label you want onto the tickmarks.
We’ll use this base graph to demonstrate.
Changing Tick Placement
Let’s say that we want a finer scale for our x-axis, so we need to add more ticks. First we might want to run a summary command to see the range of our data.
From this we can see that the smallest value is 32.1 and the largest is 59.6. So, let’s set our axis to run from 30 to 65 and place tick marks every 5mm. The function to help us do this is scale_x_continuous because our x-axis data is continuous (made of real numbers). breaks dictates where the ticks will go and we are using the function seq which has the syntax seq(start_value, end_value, by = interval).
We can also just set exact breaks using the same command. This will put the ticks at whatever number I specify.
That looks terrible, but you could have just dictated equal intervals.
I could also give the ticks different names than the default by adding labels.
This graph looks okay, but to improve it I might want to rotate the axis labels and probably drop the minor gridlines so that the delineations are only at my ticks. You should know how to rotate and you can look at the ‘Graphing Basics’ workshop for changing the gridlines.
You can do everything we just did on the x-axis to the y-axis just by changing any x command to y.
Extending Graph Dimensions
Let’s say that we don’t like how close our data is to the edges of the graph and we want to add a little bit of buffer. We can do this by changing the limits of the axes.
Here’s our basic graph with buffering on both axes.
You can use those commands in conjunction with the breaks and labels from above to customize as much as you’d like.
Changing Tick Size
Let’s change our tick marks to be more prominent. We do this with theme() again. First we’ll make them thicker and blue. Instead of element_text() we use element_line().
Now we also need to make them longer. This is a little bit different; now we need to specify units for how long we want them.
You probably notice that the ticks are just extending outward from the graph and not overlapping it. To me, this is an annoying feature of ggplot. But fear not, if you really, really want your tick marks to overlap the graph there is a way to do that. Pretty much whatever you want to do to your graphs you can do, but sometimes it takes more work than you want to put in. Here’s a helpful person on Stack Overflow explaining how to get the ticks to overlap. Basically, you can draw lines on the graph wherever you want, so you just specify where you want your short lines to go.