R Language Base Plotting Basic Plot


Example

A basic plot is created by calling plot(). Here we use the built-in cars data frame that contains the speed of cars and the distances taken to stop in the 1920s. (To find out more about the dataset, use help(cars)).

plot(x = cars$speed, y = cars$dist, pch = 1, col = 1, 
     main = "Distance vs Speed of Cars", 
     xlab = "Speed", ylab = "Distance")

enter image description here

We can use many other variations in the code to get the same result. We can also change the parameters to obtain different results.

with(cars, plot(dist~speed, pch = 2, col = 3, 
     main = "Distance to stop vs Speed of Cars", 
     xlab = "Speed", ylab = "Distance"))

enter image description here

Additional features can be added to this plot by calling points(), text(), mtext(), lines(), grid(), etc.

plot(dist~speed, pch = "*", col = "magenta", data=cars,
     main = "Distance to stop vs Speed of Cars", 
     xlab = "Speed", ylab = "Distance")
mtext("In the 1920s.")
grid(,col="lightblue")

enter image description here