<aside> ❗ In case you get stuck with this tutorial, the Q&A: Making visualisations with the Grammar of Graphics live session on Wednesday 19 October (between 15h30 and 16h30) is a good occasion to pose your questions. But you can always add your questions directly as comments in this module.

</aside>

What is ggplot2?

ggplot2 is a package for the R programming language to make visualisations. It is based on the Grammar of Graphics: the “gg” in the package’s name stands for Grammar of Graphics.

The ggplot2 package is part of an ecosystem of R packages that share the same philosophy and approach to working with data and making visualisations. This group of packages is called the tidyverse, and tidy data is a fundamental part of these packages (see the Tidy data module for more information about tidy data).

ggplot2 was developed by statistician Hadley Wickham, and for many R users the package has replaced the native plotting functionality built into the R language.

Getting started with ggplot2

In order to make visualisations with ggplot2, you need an environment to write and run R code. For this tutorial there are 2 options to do so.

The first option is to download and install RStudio. RStudio is an application that was developed to make working with R code easy. It is an integrated development environment (IDE), which means it does things like managing your files, viewing data tables and visualisations and detecting errors in your code.

To use RStudio, you need to install R first. You can download R from this page. Once R is installed on your computer, you can download RStudio from this page and install it too.

If you can’t or don’t want to install new programs on your computer, you can use RStudio Cloud. RStudio Cloud is an online version of RStudio which runs in your browser. To use RStudio, the only thing you have to do is to create a (free) account for it. You can create an account for RStudio on this page.

Once RStudio is installed and opened, or when you have logged into RStudio Cloud and created a first work space on it, you will see the RStudio interface.

Source: Maarten Lambrechts, CC BY SA 4.0

Source: Maarten Lambrechts, CC BY SA 4.0

The RStudio interface is divided into 4 main panels. The panel in the top left is called the source, and this is where you write and edit R scripts.

Source: Maarten Lambrechts, CC BY SA 4.0

Source: Maarten Lambrechts, CC BY SA 4.0

If the source pane is not visible initially, you can click File > New file > R script, or click the green “+” button and select “R script” to open a new empty R script in the source pane. You can have multiple R scripts open simultaneously, because the source pane uses tabs. The source pane is also used to preview data.

In the top right, you will find the environment pane.

Source: Maarten Lambrechts, CC BY SA 4.0

Source: Maarten Lambrechts, CC BY SA 4.0

In the environment pane you will find an overview of all the objects currently loaded in your R session. It also contains a history of all the commands you have previously run, and a wizard for importing data (click “Import Dataset” for this).

In the bottom left, you will find the console pane.

Source: Maarten Lambrechts, CC BY SA 4.0

Source: Maarten Lambrechts, CC BY SA 4.0