An effective and accurate data visualization is an important part of a statistical analysis. It can make your data come to life and convey your message in a powerful way.
R has very strong graphics capabilities that can help you visualize your data.
The plot() function
In R, the base graphics function to create a plot is the plot()
function. It has many options and arguments to control many things, such as the plot type, labels, titles and colors.
Syntax
The syntax for the plot()
function is:
plot(x,y,type,main,xlab,ylab,pch,col,las,bty,bg,cex,…)
Parameters
Parameter | Description |
x | The coordinates of points in the plot |
y | The y coordinates of points in the plot |
type | The type of plot to be drawn |
main | An overall title for the plot |
xlab | The label for the x axis |
ylab | The label for the y axis |
pch | The shape of points |
col | The foreground color of symbols as well as lines |
las | The axes label style |
bty | The type of box round the plot area |
bg | The background color of symbols (only 21 through 25) |
cex | The amount of scaling plotting text and symbols |
… | Other graphical parameters |
Create a Simple Plot
To get started with plot, you need a set of data to work with.
Let’s consider the built-in pressure dataset as an example dataset. It contains observations of the vapor pressure of mercury over a range of temperatures.
# First six observations of the 'Pressure' datasethead(pressure) temperature pressure1 0 0.00022 20 0.00123 40 0.00604 60 0.03005 80 0.09006 100 0.2700
To create a plot just specify the dataset in plot()
function.
# Plot the 'pressure' datasetplot(pressure)
![R plot() Function - Learn By Example (1) R plot() Function - Learn By Example (1)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot.png)
Change the Shape and Size of the Points
You can use the pch (plotting character) argument to specify symbols to use when plotting points.
Here’s a list of symbols you can use.
![R plot() Function - Learn By Example (2) R plot() Function - Learn By Example (2)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/r-plot-symbols.png)
# Change the shape of the pointsplot(pressure, pch=17)
![R plot() Function - Learn By Example (3) R plot() Function - Learn By Example (3)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot01.png)
For symbols 21 through 25, you can specify border color using col argument and fill color using bg argument.
# Change the border color to blue and background color to lightblueplot(pressure, pch=21, col="blue", bg="lightblue")
![R plot() Function - Learn By Example (4) R plot() Function - Learn By Example (4)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot02.png)
To alter the size of the plotted characters, use cex (character expansion) argument.
# Scale the data points by 1.2plot(pressure, cex=1.2)
![R plot() Function - Learn By Example (5) R plot() Function - Learn By Example (5)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot03.png)
Changing the Color
You can change the foreground color of symbols using the argument col.
# Change the color of symbols to redplot(pressure, col="red")
![R plot() Function - Learn By Example (6) R plot() Function - Learn By Example (6)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot04.png)
R has a number of predefined colors that you can use in graphics. Use the colors()
function to get a complete list of available names for colors.
# List of predefined colors in Rcolors()[1] "white" "aliceblue" "antiquewhite" [4] "antiquewhite1" "antiquewhite2" "antiquewhite3"...
Or you can refer the following color chart.
![R plot() Function - Learn By Example (7) R plot() Function - Learn By Example (7)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/r-color-chart.png)
You can specify colors by index, name, hexadecimal, or RGB value. For example col=1
, col="white"
, and col="#FFFFFF"
are equivalent.
Different Plot Types
You can change the type of plot that gets drawn by using the type argument.
Here’s a list of all the different types that you can use.
Value | Description |
“p” | Points |
“l” | Lines |
“b” | Both points and lines |
“c” | The lines part alone of “b” |
“o” | Both points and lines “overplotted” |
“h” | Histogram like (or high‐density) vertical lines |
“s” | Step plot (horizontal first) |
“S” | Step plot (vertical first) |
“n” | No plotting |
For example, to create a plot with lines between data points, use type="l"
; to draw both lines and points, use type="b"
.
A series of graphics showing different types is shown below.
![R plot() Function - Learn By Example (8) R plot() Function - Learn By Example (8)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot16.png)
Adding Titles and Axis Labels
You can add your own title and axis labels easily by specifying following arguments.
Argument | Description |
main | Main plot title |
xlab | x-axis label |
ylab | y-axis label |
plot(pressure, main = "Vapor Pressure of Mercury", xlab = "Temperature (deg C)", ylab = "Pressure (mm of Hg)")
![R plot() Function - Learn By Example (9) R plot() Function - Learn By Example (9)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot05.png)
The Axes Label Style
By specifying the las (label style) argument, you can change the axes label style. This changes the orientation angle of the labels.
Value | Description |
0 | The default, parallel to the axis |
1 | Always horizontal |
2 | Perpendicular to the axis |
3 | Always vertical |
For example, to change the axis style to have all the axes text horizontal, use las=1
plot(pressure, las = 1)
![R plot() Function - Learn By Example (10) R plot() Function - Learn By Example (10)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot06.png)
The Box Type
Specify the bty (box type) argument to change the type of box round the plot area.
Value | Description |
“o” | (default) Draws a complete rectangle around the plot. |
“n” | Draws nothing around the plot. |
“l”, “7”, “c”, “u”, or “]” | Draws a shape around the plot area. |
# Remove the box round the plotplot(pressure, bty="n")
![R plot() Function - Learn By Example (11) R plot() Function - Learn By Example (11)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot07.png)
Add a Grid
The plot()
function does not automatically draw a grid. However, it is helpful to the viewer for some plots. Call the grid()
function to draw the grid once you call the plot()
.
plot(pressure)grid()
![R plot() Function - Learn By Example (12) R plot() Function - Learn By Example (12)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot08.png)
Add a Legend
You can include a legend to your plot – a little box that decodes the graphic for the viewer. Call the legend()
function, once you call the plot()
.
# Add a legend to the top left cornerplot(pressure, col="red", pch=19)points(pressure$temperature/2, pressure$pressure,col="blue", pch=17)legend("topleft", c("line 1","line 2"), pch=c(19,17), col=c("red","blue"))
![R plot() Function - Learn By Example (13) R plot() Function - Learn By Example (13)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot09.png)
The position of the legend can be specified using the following keywords : “bottomright”, “bottom”, “bottomleft”, “left”, “topleft”, “top”, “topright”, “right” and “center”.
The effect of using each of these keywords is shown below.
![R plot() Function - Learn By Example (14) R plot() Function - Learn By Example (14)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/r-legend-positions.png)
Add Points to a Plot
You can add points to a plot with the points()
function.
For example, let’s create a subset of pressure containing temperatures greater than 200 °C and add these points to the plot.
plot(pressure, col = "red")points(pressure[pressure$temperature > 200, ], col = "red", pch = 19)
![R plot() Function - Learn By Example (15) R plot() Function - Learn By Example (15)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot10.png)
Add Lines to a Plot
You can add lines to a plot in a very similar way to adding points, except that you use the lines()
function to achieve this.
plot(pressure)lines(pressure$temperature/2, pressure$pressure)
![R plot() Function - Learn By Example (16) R plot() Function - Learn By Example (16)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot11.png)
You can change the line type using lty argument; and the line width using lwd argument.
# Change the line type and line widthplot(pressure)lines(pressure$temperature/2, pressure$pressure, lwd=2, lty=3)
![R plot() Function - Learn By Example (17) R plot() Function - Learn By Example (17)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot12.png)
Here’s a list of line types you can use.
![R plot() Function - Learn By Example (18) R plot() Function - Learn By Example (18)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/r-line-types.png)
There’s another function called abline()
which allows you to draw horizontal, vertical, or sloped lines.
# Draw a dotted horizontal line at 247 and vertical line at 300plot(pressure)abline(h= 247, v=300, col="red", lty=2)
![R plot() Function - Learn By Example (19) R plot() Function - Learn By Example (19)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot13.png)
Label Data Points
Use the text()
function to add text labels at any position on the plot.
The position of the text is specified by the pos argument. Values of 1, 2, 3 and 4, respectively places the text below, to the left of, above and to the right of the specified coordinates.
# Add text labels above the coordinatesplot(pressure, pch=19, col="red")text(pressure, labels=pressure$pressure, cex=0.7, pos=3, col="blue")
![R plot() Function - Learn By Example (20) R plot() Function - Learn By Example (20)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot14.png)
Set Axis Limits
By default, the plot()
function works out the best size and scale of each axis to fit the plotting area. However, you can set the limits of each axis quite easily using xlim and ylim arguments.
# Change the axis limits so that the x-axis and y-axis ranges from 0 to 500plot(pressure, ylim=c(0,500), xlim=c(0,500))
![R plot() Function - Learn By Example (21) R plot() Function - Learn By Example (21)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot15.png)
Display Multiple Plots on a Single Page
By using the mfrow
graphics parameter, you can display multiple plots on the same graphics page.
To use this parameter, you need to pass a two-element vector, specifying the number of rows and columns. Then fill each cell in the matrix by repeatedly calling plot.
For example, mfrow=c(1, 2)
creates two side by side plots.
par(mfrow = c(1, 2))plot(cars, main="Speed vs Distance", col="red")plot(mtcars$mpg, mtcars$hp, main="HP vs MPG", col="blue")
![R plot() Function - Learn By Example (22) R plot() Function - Learn By Example (22)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot17.png)
Once your plot is complete, you need to reset your par()
options. Otherwise, all your subsequent plots will appear side by side.
# Reset the mfrow parameterpar(mfrow = c(1,1))
Save a Plot to an Image File
To save a plot to an image file, you have to do three things in sequence:
- Call a function to open a new graphics file, such as
png()
,jpg()
orpdf()
. - Call
plot()
to generate the graphics image. - Call
dev.off()
to close the graphics file.
# Save a plot as a png filepng(filename="myPlot.png", width=648, height=432)plot(pressure, col="slateblue1", pch=19, type="b", main = "Vapor Pressure of Mercury", xlab = "Temperature (deg C)", ylab = "Pressure (mm of Hg)")dev.off()
myPlot.png
![R plot() Function - Learn By Example (23) R plot() Function - Learn By Example (23)](https://i0.wp.com/www.learnbyexample.org/wp-content/uploads/r/rplot18.png)
Remember that the file will be saved to your current working directory, unless you specify an absolute file path.