Ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geoms—visual marks that represent data points, and a coordinate system. There are many scenarios where we need to annotate outside the plot area or specific area as per client requirements. In this case, the ggplot2 library comes very handy with its sub-options to get the required output and with good customization options for data visualizations.
To add annotations in R using ggplot2, annotate() function is used.
- geom : specify text
- x : x axis location
- y : y axis location
- label : custom textual content
- color : color of textual content
- size : size of text
- fontface : fontface of text
- angle : angle of text
- Import library
- Create or load dataset
- Create a normal plot
- Add annotate() function with required parameters
Let us first see how annotations are added inside the plot, so that the difference in position of the annotations can be understood better.
Now let us visualize with annotations outside the plot.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.