ggplot2 builds charts through layers using
geom_ functions. Here is a list of the different available geoms. Click one to see an example using it.
Annotation is a key step in data visualization. It allows to highlight the main message of the chart, turning a messy figure in an insightful medium.
ggplot2 offers many function for this purpose, allowing to add all sorts of text and shapes.
Annotate with geom_text
geom_text() allows to add annotation to one, several or all markers of your chart.
Annotate with geom_label
Very close to geom_text, geom_label produces a label wrapped in a rectangle. This example also explains how to apply labels to a selection of markers.
Learn how to use the annotate function to add a rectangle on a specific part of the chart.
Marginal plots are not natively supported by
ggplot2, but their realisation is straightforward thanks to the
ggExtra library as illustrated in graph #277.
Scatterplot with rug
Add rug on X and Y axis to describe the numeric variable distribution. Show how geom_rug() works.
Add marginal distribution around your scatterplot with ggExtra and the ggMarginal function.
theme() function of
ggplot2 allows to customize the chart appearance. It controls 3 main types of components:
When working with categorical variables (= factors), a common struggle is to manage the order of entities on the plot.
Post #267 is dedicated to reordering. It describes 3 different way to arrange groups in a
ggtitle() function allows to add a title to the chart. The following post will guide you through its usage, showing how to control title main features: position, font, color, text and more.
Small multiples is a very powerful dataviz technique. It split the chart window in many small similar charts: each represents a specific group of a categorical variable. The following post describes the main use cases using
facet_grid() and should get you started quickly.
It is possible to customize any part of a
ggplot2 chart thanks to the
theme() function. Fortunately, heaps of pre-built themes are available, allowing to get a good style with one more line of code only. Here is a glimpse of the available themes. See code
plotly: turn your
Another awesome feature of
ggplot2 is its link with the
plotly library. If you know how to make a
ggplot2 chart, you are 10 seconds away to rendering an interactive version. Just call the
ggplotly() function, and you’re done. Visit the interactive graphic section of the gallery for more.
library(ggplot2) library(plotly) library(gapminder) p <- gapminder %>% filter(year==1977) %>% ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) + geom_point() + theme_bw() ggplotly(p)
← this chart is interactive: hover, drag, zoom, export and more.
Each section of the gallery provides several examples implemented with
ggplot2. Here is an overview of my favorite examples:
Violin + boxplot + sample size
This examples provides 2 tricks: one to add a boxplot into the violin, the other to add sample size of each group on the X axis
Map marker feature to variable
Ggplot2 makes it a breeze to map a variable to a marker feature. Here is an example where marker color depends on its category.
Make your chart pretty with nice color scale, general theme, stroke around cirle and more..
Improve general appearance
Add title, use a theme, change color palette, control variable orders and more
Change shape parameters depending on where the data points is located compared to a threshold.
Small multiple is probably the best alternative, making obvious the evolution of each gropup.
Sometimes programming can be used to generate figures that are aestetically pleasing, but don't bring any insight. Here are a few pieces of data art built from R and ggplot2. Visit data-to-art.com for more.