More ggplot

In this and the next few lessons we return to ggplot, which we explored back in the Completing Our First Plot lesson. In the remainder of this and the next two lessons we will learn more plotting techniques to level up our data visualization skills.

Scatter plot with geom_point

Let's review what we have already learned about plotting with ggplot.

In the following scatter plot, we set Sepal.Length on the x-axis, set Petal.Length on the y-axis, colour in the points based on their Species, and produce a scatter plot with the geom_point function.

R interactive coding assignment

Let's go over each component in the code block to recap what everything signifies.

  • ggplot: Is the function that makes the plot.
  • data = iris: Is the first argument in the ggplot function and defines the data that can be used in the plot.
  • aes: Stands for aesthetic and is the argument that we use to define the x-axis and y-axis.
  • x = Sepal.Length: Sets the Sepal.Length variable as the x-axis.
  • y = Petal.Length: Sets the Petal.Length variable as the y-axis.
  • colour = Species: Colours the points based on the Species column.
  • +: ggplot defines the base ggplot object, we add plots to it using the plus sign.
  • geom_point(): This geom function defines the type of plot ggplot will display, in this case, a scatter plot.

Plot Annotations

We may add a plot title and axis labels using the labs function. This function is used to modify the labels of various elements in a plot. Adding text to a plot is referred to as annotating it. It allows customization of the labels for the x-axis (the x parameter), y-axis (the y parameter), and the title (the title parameter) and much more.

R interactive coding assignment