For the examples on this page, you’ll want to require the openintro package. Load the package into working memory. You must do this each time you use RStudio.

require(openintro)

Create a scatter plot

The cars data set (part of the openintro package) contains information about 54 cars from 1993,

We can use the R function plot() to create a scatter plot for the variables weight and mpgCity variables. In the plot() function, the first argument should be yvariable~xvariable and the second argument should state the name of the data set.

plot(mpgCity~weight, data = cars)

Like with other plotting functions, we can modify the command to create titles. I also like to change the dots to filled-in circles using the pch argument.

plot(mpgCity~weight, # Specify the variables to graph
        data = cars, #Specify the data set
        pch = 19, #Changes the plotting symbol
        main = "Cars in 1993", # Create a title for the plot
        xlab = "Weight in pounds", #Create the x-axis label
        ylab = "Miles per gallon") #Create the y-axis label