How To Annotate a Graph within ggplot2

Adding Words to fit the Data

When we use the word “annotations”, we primarily mean text. Working in data science, there’s always a need to make your data more easily readable to others if nothing else than to effectively communicate your results and share insights. R programming uses the ggplot function geom to create an annotation layer to your line graph enabling full-text annotation on multiple lines to make your dataframe fully readable by your audience. In the following paragraphs, we’ll look at ggplot and its role in aesthetic mapping your plot and annotate the key points of the dataframe. We’ll look at codes addressing assigning aesthetic values like color, line type, and positioning concerning the rest of the visual graphics. We’ll also analyze breaking a plot into smaller segments annotating individually to give them their discernable aesthetic. Lastly, we’ll look at how we can utilize these new functions and where you can go to learn more.

Starting on the Horizontal Line

The r programming lists many functions under ggplot as geom to discern values along the x-axis. Use the command geomhline or geom path when connecting each of your plotted values in the order they occur so the end visual a nice steady line showing the trending data set.

library(ggplot2)

ggplot(data=df, aes(x=dose, y=len, group=1)) +
geom_line()+
geom_point()

ggplot(data=df, aes(x=dose, y=len, group=1)) +
geom_line(linetype = “dashed”)+
geom_point()

ggplot(data=df, aes(x=dose, y=len, group=1)) +
geom_line(color=”red”)+
geom_point()

When using ggplot, make note that you will have a lot of arguments for every annotation added. Aspects like color, line type, and highlighting need to be addressed as missing values will lead to bad visuals. If you need to add to your plot, the geom_annotate function will take the given values and add them to your existing layer.

Here’s a sample that will add text
library(ggplot2)

p ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()

annotation data.frame(
x = c(2,4.5),
y = c(20,25),
label = c(“label 1”, “label 2”)
)

p + geom_text(data=annotation, aes( x=x, y=y, label=label), ,
color=”orange”,
size=7 , angle=45, fontface=”bold” )

p + geom_label(data=annotation, aes( x=x, y=y, label=label), ,
color=”orange”,
size=7 , angle=45, fontface=”bold” )
This will take information from the data frame to place annotations highlighting certain values, as well as what color the lines and highlights should be. As a side note, its possible to compress all these commands into a few lines:

annotate(“text”, x = c(2,4.5), y = c(20,25),
label = c(“label 1”, “label 2″) , color=”orange”,
size=7 , angle=45, fontface=”bold”)
These shortcuts make working on multiple plots or even a heavily annotated line plot. What’s more, is after getting the basics of the annotation function, you can polish your final presentation with a few more commands to make it stand out.

Smooth as Geom

Looking at data-heavy graphics can be a strain, especially when trying to emphasize a key feature or trend and your audience is lost on the intent. The beauty of the geom smooth command is its inherent ability to apply effects on the graph once given values. The only catch will be in discerning orientation as the function plots differently for the y and x-axis. Its default grouping aside, you only have to put in your parameters in special cases, like t-based approximating in loess(). Here’s a set of values for geom smooth to plot along the x-axis
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth()
By designating different orientation, geom can plot on the y axis for a completely different visual:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(orientation = “y”)
For this example, we’ll look at function loess, where the span aspect comes into play where the amount of wiggle in your line can vary based on value.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(span = 0.3)
A span of 3 is nothing special, but these parameters can be customized on your preference at any time.
What’s more, you don’t have to use the same function to do the same job.
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(method = lm, se = FALSE)
This example uses the difference between y and x to create a trend line, while the smooth function has no allowed wiggle and makes a clean graphic with no variation. Depending on what visual aesthetic you want to reach, you can adopt any of these functions and use them as much as you want to create a portfolio of data representation.

Overriding the Group

The ggplot packages all group their aesthetics by default, making for the occasional grouping in a larger graphic as variables are categorized and grouped by the function automatically. While usually not an issue, if you want to create certain plot types like a profile type or an interactive points-based preference, then we need to outline what variables we want connecting and shown on our visual.
If we take a boxplot with discrete variables (Occasion and height), the Occasion variable gets its x scale for each variable. For the example we have;

ggplot(Oxboys, as(Occasion, height)) +
geom_boxplot()
we want to override the default grouping and draw lines crossing each value for the variable. By adding geom lines with specified variables, we can draw over the current box plot and connect them to create our preferred plot.

ggplot(Oxboys, aes(Occasion, height)) +
geom_boxplot() +
geom_line(aes(group = Subject), colour = “#3366FF”, alpha = 0.5)

Annotate In Your Toolset

So now that you have a hand at annotating your visuals, your next presentation can be a fully outfitted visual pleasure for the eyes of your audience and help draw their attention to your findings. Controlling the many aspects of color and line type might seem daunting, but each one is an additional control for you to add personification in your style to what would otherwise be a humble set of numbers and plots. Look to utilize the ggplot2 package for your next graphic assignment and see how far you can push the envelope in outlining trends for the best graphical proof of your arguments. To read more about annotations and get ideas for your next project, look to some of these articles and try your hand at exercises, and always keep learning to master your job:

https://www.urc-chs.com/projects/assist/https://mode.com/blog/r-ggplot-extension-packages/http://www.sthda.com/english/wiki/ggplot2-essentials
Scroll to top