This lesson is being piloted (Beta version)

R Basic Plotting system

Overview

Teaching: 5 min
Exercises: 0 min
Questions
  • How to make a quick and simple plot in R

Objectives
  • Basic R plot

The Base plotting system

Important BASE graphic parameters

pch: the plotting symbol (default is open circle)
lty: the line type (default is solid line), can be dashed, dotted, etc.
lwd: the line width, specified as an integer multiple
col: the plotting color, specified as a number, string, or hex code; the colors() function gives you a vector of colors by name
xlab: character string for the x-axis label
ylab: character string for the y-axis label

Important BASE plotting function

plot: make a scatterplot, or other type of plot depending on the class of the object being plotted
lines: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots
points: add points to a plot
text: add text labels to a plot using specified x, y coordinates
title: add annotations to x, y axis labels, title, subtitle, outer margin
mtext: add arbitrary text to the margins (inner or outer) of the plot
axis: adding axis ticks/labels

Motor Trend Car Road Tests example

The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

data(mtcars)
dim(mtcars)
names(mtcars)
head(mtcars)
print(mtcars)
factor(mtcars$cyl)
boxplot(mpg~cyl,data=mtcars,
        col=terrain.colors(3),main="MPG by car cylinders",
        xlab = "cylinders",ylab="mpg")
legend("topright",c("4","6","8"),fill = terrain.colors(3))

image

Histograms

hist(mtcars$hp, col="magenta")

image

Barplot

barplot(mtcars$mpg,col="green",
        main="MPG for 32 cars")

image

Scatter plot

plot(mtcars$mpg,mtcars$wt,main="Car Fuel vs Weight",
     xlab="Milage per Gallon",ylab="Weight",
     col = mtcars$cyl,pch=16,cex=3)
legend("topright",legend=c(8,6,4),pch=16,cex=3,
       col=c("grey","magenta","blue"))

image

Subplot

par(mfrow=c(2,2))
hist(mtcars$mpg,col="blue")
boxplot(mpg~cyl,data=mtcars,
        col=terrain.colors(3),main="MPG by car cylinders",
        xlab = "cylinders",ylab="mpg")
legend("topright",c("4","6","8"),fill = terrain.colors(3))

plot(mtcars$mpg,mtcars$wt,main="Car Fuel vs Weight",
     xlab="Milage per Gallon",ylab="Weight",
     col = mtcars$cyl,pch=16,cex=3)
barplot(mtcars$mpg,col="green",
        main="MPG for 32 cars")     

image

Graphics Devices

A graphics device is something where you can make a plot appear When you make a plot in R, it has to be “sent” to a specific graphics device.

The most common place for a plot to be “sent” is the screen device

dev.copy(png,"filename.png") # to save the image to file
dev.off() # to close all the graphical devices

Key Points