Specifying a dataset

ggplot(___) + 
    mapping = aes(x = ___, y = ___)
View Interactive Version

To create plots with ggplot2 you first need to load the package using library(ggplot2).

After the package has been loaded specify the dataset to be used as an argument of the ggplot() function. For example, to specify a plot using the cars dataset you can use:


Note that this command does not plot anything but a grey canvas yet. It just defines the dataset for the plot and creates an empty base on top of which we can add additional layers.

Create a scatter plot with ggplot