We can create a variety of visualizations using plot(). plot() is a base R function for creating plots. Later in the course, we will focus heavily on visualization with ggplot2, however, using base R plots is still useful in making simple graphs!

plot() has a variety of arguments we can use:

x – the x-axis variable (independent variable)

y – the y-axis variable (optional; dependent variable)

xlab – the label on the x-axis

ylab – the label on the y-axis

main – the title of the plot

There are quite a few more that can be found in the help text for plot! (?plot)

Let’s load in a simple data frame we want to make a plot of. It contains 2 numeric data (mass_g and length_cm) and a character data (species)

df <- data.frame(mass_g = c(10,20,10,15,10,10,15,20,11,10,12,11),
                 length_cm = c(11,12,15,12,11,13,14,15,11,13,12,13),
                 species = c("Species_A", "Species_A","Species_A","Species_A","Species_B","Species_B","Species_B","Species_B",
                 "Species_C","Species_C","Species_C", "Species_C"))

Scatter plot

Let’s make a simple scatter plot of length and mass. Scatter plots assume that both x and y are numeric data. We assume mass is dependent on length, thus length goes on the x-axis.

Note: To access data within a data frame, we use the $ next to the name of the object. This is covered in more detail in Section 2.

plot(x = df$length_cm, y = df$mass_g)

Image of a basic scatter chart. X axis reads df$length_cm with tick marks left to right 11, 12, 13, 14, 15.  y axis reads  df$mass_g with tick marks bottom to top, 10,12,14,15,18,20. Points are scattered throughout showing no discernable pattern

Simple! Let’s now add in the title, x-axis label, and y-axis label, using the arguments we mentioned.

plot(x = df$length_cm, y = df$mass_g,
     xlab = "Length (cm)", ylab = "Mass (g)",
     main = "Relationship of Length and Mass")

Image of a basic scatter chart. X axis reads Length (cm) with tick marks left to right 11, 12, 13, 14, 15.  y axis reads  Mass (g) with tick marks bottom to top, 10,12,14,15,18,20. Points are scattered throughout showing no discernable pattern. Title reads Relationship of Length and Mass

As a reminder, each argument needs to be separated by a comma and all arguments need to be within the parentheses.

Box plot

We create box plots when we have character and numeric data. Let’s create one for the variables mass_g and species!

To do so, we use the boxplot() function. boxplot() uses similar arguments as plot(), the only difference is that the data input uses a format of y~x. y is still our dependent variable, while x is our independent variable.

boxplot(df$mass_g ~ df$species)

Boxplot with 3 boxes. X axis labeled df$species with 3 tick makrs Species_A, Species_B and Species_c. Y axis reads df$mass_g with tick marks 10,12,14,16,18,20. The bars for Species_A and B are similar, and span from 10 to ~18 while species C spans from 10-11

As mentioned earlier, we can specify our labels and title using the same arguments as plot(). Let’s add those now.

boxplot(df$mass_g ~ df$species, main = "Masses of different species",
        xlab = "Species", ylab= "Mass in grams")

Boxplot with 3 boxes. X axis labeled Species with 3 tick makrs Species_A, Species_B and Species_c. Y axis reads Mass in grans with tick marks 10,12,14,16,18,20. The bars for Species_A and B are similar, and span from 10 to ~18 while species C spans from 10-11. Title reads Masses of different species

This has been just a brief introduction to plots! Once you want to start making more complex plots, I highly recommend learning the ggplot2 package.

Last modified: Tuesday, 2 December 2025, 11:39 AM