Data visualization with R and ggplot2 | the R Graph Gallery (2024)


Data visualization with R and ggplot2 | the R Graph Gallery (1)

ggplot2 is a R package dedicated to data visualization. It can greatly improve the quality and aesthetics of your graphics, and will make you much more efficient in creating them.

ggplot2 allows to build almost any type of chart. The R graph

gallery focuses on it so almost every section there starts with ggplot2 examples.

This page is dedicated to general ggplot2 tips that you can apply to any chart, like customizing a title, adding annotation, or using faceting.

If you love ggplot2, you will love my productive r workflow project where I show how it interacts with Quarto, Git and Github! ❤️

A world of geom

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.

geom_bar geom_bin geom_boxplot geom_density geom_error geom_hex geom_hist geom_hline geom_jitter geom_label geom_line geom_point geom_polygon geom_rect geom_ribbon geom_rug geom_segment geom_smooth geom_text geom_tile geom_violin geom_vline

Annotation with ggplot2

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.
Rectangle Learn how to use the annotate function to add a rectangle on a specific part of the chart.
Segment How to add one or several segments to the chart.
Arrow Basically the same as for a segment, with just one additional argument.
Abline Add horizontal or vertical ablines with geom_hline and geom_vline.

Marginal plot

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.
Marginal distribution Add marginal distribution around your scatterplot with ggExtra and the ggMarginal function.
Marginal boxplot Using boxplots is another way to show the marginal distribution. Find more in this post.

ggplot2 chart appearance

The theme() function of ggplot2 allows to customize the chart appearance. It controls 3 main types of components:

  • Axis: controls the title, label, line and ticks
  • Background: controls the background color and the major and minor grid lines
  • Legend: controls position, text, symbols and more.
Axis Customize ggplot2 axis: title, line, labels and ticks.
Background Customize ggplot2 background: color, major and minor grid lines.
Legend Customize ggplot2 legend: position, title, text, key symbol and more.

Re-ordering with ggplot2


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 ggplot2 chart:

  • Using the forcats package
  • With dplyr
  • With the reorder() function of base R

Read post

ggplot2 title

The 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.

Basic Learn how to add a title with ggtitle, default options.
Appearance Learn how to change title font, size, color, text and more.
Position Check how to change title position.

Use custom fonts with ggplot2

If you don't want your plot to look like any others, you'll definitely be interested in using custom fonts for your title and labels! This is totally possible thanks to 2 main packages: ragg and showtext. The blog-post below should help you using any font in minutes.

Custom font Learn how to add a custom font using the ragg or the showtext library in a ggplot2 chart.

Small multiples: facet_wrap() and facet_grid()

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_wrap() and facet_grid() and should get you started quickly.

Facet_wrap Facet wrap allows to build small multiples using one categorical variable.
Facet_grid Same idea, but using 2 categorical variables for the faceting.
Customized An advanced examples to make sure you know how to make your small multiple pretty.
Strip features Customize the general layout with the strip option.
Mixing charts How to combine several charts together with ggplot2.

A set of pre-built themes

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


→ ggplot2

default theme_bw() theme_minimal() theme_classic() theme_gray()

→ ggthemes

theme_excel() theme_economist() theme_fivethirtyeight() theme_tufte() theme_gdocs() theme_wsj() theme_calc() theme_hc()

→ other

theme_article() theme_pubclean() theme_bigstatsr() theme_ipsum()

Data visualization with R and ggplot2 | the R Graph Gallery (24)Data visualization with R and ggplot2 | the R Graph Gallery (25)

plotly: turn your ggplot interactive

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.

An overview of ggplot2 possibilities

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
Add individual observation See how low group C sample size actually is?
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.
Improve appearance Make your chart pretty with nice color scale, general theme, stroke around cirle and more..
Custom appearance Classic appearance customization with theme_ipsum and more
Improve general appearance Add title, use a theme, change color palette, control variable orders and more
Conditional color Change shape parameters depending on where the data points is located compared to a threshold.
Cleveland dot plot A variation of the lollipop chart to study several categories on the same chart.
Customization Add scale, group names and more
Customization Usual customizations like title, theme, color palette and more.

Data art

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.

Droid A BB8 droid made using R and ggplot2.
Random Shapes A piece of generative art built with R and ggplot2.
R Snail R Snail, a generative figure made by Christophe Cariou, built with R.

Related chart types

Ggplot2

Animation

Interactivity

3D

Data visualization with R and ggplot2 | the R Graph Gallery (44)

Caveats

Data visualization with R and ggplot2 | the R Graph Gallery (45)

Data art

Data visualization with R and ggplot2 | the R Graph Gallery (2024)

FAQs

What is ggplot in data visualization? ›

In this article you will understand ggplot2 code and how to use ggplot2. ggplot2 is a powerful data visualization package in R that uses a consistent grammar of graphics. By learning ggplot2 code, you can create beautiful, customizable plots to explore and present your data.

How to plot data in R using ggplot2? ›

7 Plotting with ggplot2
  1. Start by preparing a dataset so that it is in the right format.
  2. Create a plot object using the function ggplot() .
  3. Define so-called “aesthetic mappings”, i.e. we determine which variables should be displayed on the X and Y axes and which variables are used to group the data.

What library is used for data visualization in R? ›

Plotly is an R package library for all your graphics needs, and it is open-source and free to use. Using plotly, developers can create remarkably beautiful and interactive visualizations.

What is the difference between ggplot and ggplot2? ›

You complete your graph by adding one or more layers to ggplot() . The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot. You’ll learn a whole bunch of them throughout this chapter.

What is the difference between ggplot and R plot? ›

The graph made by ggplot is more clear. The default color is not grey and black and we can see the labels in x direction and we also have legends by default. However, the graph made by normal r packages doesn't have default legends and x labels.

What does ggplot2 stand for? ›

ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics]. Some documentation can be found on the ggplot website .

Why is ggplot used in R? ›

ggplot2 is a R package dedicated to data visualization. It can greatly improve the quality and aesthetics of your graphics, and will make you much more efficient in creating them. gallery focuses on it so almost every section there starts with ggplot2 examples.

What is data visualization in R programming? ›

Data visualization is a technique used for the graphical representation of data. By using elements like scatter plots, charts, graphs, histograms, maps, etc., we make our data more understandable. Data visualization makes it easy to recognize patterns, trends, and exceptions in our data.

Is R good for data visualization? ›

R does well for analytics and visualization. The key is to understand the problem you are trying to solve and the decision maker who needs to use the output.

Which R package should you use for data visualization? ›

Fortunately, some packages are specialized for this kind of situation, making it a breeze to build those kind of charts:
  • ggraph. igraph. chorddiag.
  • showtext. cowplot. marquee.
  • rColorBrewer. viridis. paletteer.

Which data visualization library is best? ›

Altair: Declarative visualizations in a snap

Altair is one among the most used Data Visualization Python libraries as it helps simplify the process of creating interactive visualizations because of its declarative nature.

What types of plots can be made in R using ggplot? ›

In this lesson, you will focus on using ggplot2 to create four types of graphs: a histogram, a scatter plot, a bar plot, and a box plot. However, you can create numerous other types of graphs in ggplot2, such as density plots, box plots, violin plots, maps, and much more.

What is the difference between Matplotlib and ggplot? ›

As you can see in this example already, ggplot2 and matplotlib have a fundamentally different syntactic approach. While ggplot2 works with layers and easily allows the creation of regression lines for each species, you have to use a loop to get the same results with matplotlib .

What is the difference between ggplot and Boxplot? ›

Boxplots are often used to show data distributions, and ggplot2 is often used to visualize data. A question that comes up is what exactly do the box plots represent? The ggplot2 box plots follow standard Tukey representations, and there are many references of this online and in standard statistical text books.

What is the difference between Tableau and ggplot? ›

Tableau is a standalone software with built-in data manipulation capabilities, while ggplot2 is a package in R that requires users to write code for data manipulation. Tableau has a shorter learning curve but comes with a cost, whereas ggplot2 is free and benefits from a large community support.

What are the three components of ggplot? ›

Out of these components, ggplot2 needs at least the following three to produce a chart: data, a mapping, and a layer.

References

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6398

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.