How to add details to a plot (in R)
Task
After making a plot, we might want to add axis labels, a title, gridlines, or text. Plotting packages provide tons of tools for this sort of thing. What are some of the essentials?
Related tasks:
- How to create basic plots
- How to create a histogram
- How to create a box (and whisker) plot
- How to change axes, ticks, and scale in a plot
- How to create bivariate plots to compare groups
- How to plot interaction effects of treatments
Solution
We will create some fake data using R vectors, for simplicity.
But everything we show below works also if your data is in columns of a data frame, such as df$age
.
1
2
patient_height <- c( 60, 64, 64, 65, 66, 66, 70, 72, 72, 76 )
patient_weight <- c( 141, 182, 169, 204, 138, 198, 180, 175, 244, 196 )
The following code creates a plot with many details added, but each is independent of the others, so you can take just the bit of code that you need.
1
2
3
4
5
6
7
8
9
plot( patient_height, patient_weight,
main="This is the title.",
xlab="This is the x axis label.",
ylab="This is the y axis label." ) # Scatter plot with labels
grid() # Turns on gridlines
text( 70, 200, "Text at (70,200)" ) # Add text
text( 65, 225, "Point 1", pos=2 ) # Text to the left of a point
text( 72, 244, "Point 2", pos=4 ) # Text to the right of a point
arrows( 65, 225, 72, 244, col='red' ) # Arrow from (65,225) to (72,244)
Content last modified on 24 July 2023.
See a problem? Tell us or edit the source.
Contributed by Debayan Sen (DSEN@bentley.edu)