Table of Contents
Introduction
When creating data visualizations in R, controlling the scale of the graph axes is crucial for effectively conveying information. The default scales that R applies may not always highlight patterns in the data optimally. Fortunately, R provides extensive customization options to tweak the x and y axis scales and limits.
This article explains key techniques for customizing axes in R graphs to make them more informative and visually appealing. We cover how to modify axes in both base R plots as well as ggplot2 visualizations.
Changing Axis Limits in Base R Plots
The key functions for modifying axis limits in base R plots are xlim()
and ylim()
. They can be passed as arguments to plotting functions like plot()
, hist()
, and others that create graphical output.
Here is an example of using xlim()
and ylim()
to set the x-axis from 0 to 30 and the y-axis from 0 to 150:
# Sample data
df <- data.frame(x = 1:100, y = rnorm(100))
# Plot with custom axes
plot(df$x, df$y, xlim = c(0, 30), ylim = c(0, 150))
We can also use the log
parameter to easily convert an axis to log scale. This helps visualize skewed data.
“`r
Y-axis in log scale
plot(df$x, df$y, log = “y”)
## Changing Axis Limits in ggplot2
To modify axes in ggplot2, the `scale_x_continuous()` and `scale_y_continuous()` functions are used. The syntax is similar:
r
Load ggplot2
library(ggplot2)
Sample data
df <- data.frame(x = 1:100, y = rnorm(100))
Plot with custom axes
ggplot(df, aes(x, y)) +
geom_point() +
scale_x_continuous(limits = c(0, 30)) +
scale_y_continuous(limits = c(0, 150))
We can also easily convert to log scale, just like in base R graphics:
r
ggplot(df, aes(x, y)) +
geom_point() +
scale_y_continuous(trans=’log10′)
## Changing Tick Marks and Axis Labels
The `breaks` argument is used to control the major tick marks displayed on an axis. For example, to show tick marks every 5 units on the x-axis:
r
scale_x_continuous(breaks = seq(0, 30, 5))
Axis labels can be modified with the `name` argument:
r
scale_x_continuous(name = “New X Label”)
This applies to both `scale_x_continuous()` and `scale_y_continuous()`.
## Setting Global Theme Options
Instead of tweaking axes on every plot, you can set global theme options for things like text size, font family, grid lines, etc.
In ggplot2, this is done with the `theme()` function:
r
theme(
axis.title = element_text(size = 16),
panel.grid.major.x = element_line(color = “grey60”),
plot.background = element_rect(fill = “white”)
)
“`
These specifications will apply to all subsequent plots.
Common Mistakes to Avoid
When modifying axes, some common mistakes include:
- Setting limits that crop the data instead of revealing more detail
- Using highly skewed scales that distort trends
- Not labeling properly after transforming axes
- Removing too many grid lines/tick marks so positioning along axes becomes unclear
In general, axis modifications should clarify patterns in the data rather than hide or distort them.
Additional Tips for Readability
Here are some additional tips for ensuring your axes modifications improve readability:
- Use consistent breaks for easier comparison across multiple plots
- Format tick mark labels appropriately (e.g. currency, percentages)
- Sort factors/groups meaningfully along axes
- Rotate long tick mark labels to prevent overlapping
- Consider both landscape and portrait orientations for plot layout
Conclusion
Learning how to properly tweak the graph axes goes a long way in creating meaningful data visualizations with R. Both base R and ggplot2 provide extensive options to customize your plots. Pay attention to clarity, readability, appropriate data representation, and visual appeal when modifying axes. With some practice, you’ll be able to take your R graphics to the next level.
Related Resources
- Chapter 11 – Customizing Graphs from R for Data Science
- Control axis limits and scales in Data Visualization with R
- How to Change Axis Scales in R Plots from Statology