While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
In this tutorial, let us first begin by understanding the basics using the plot() function in R. The R language is well known for its beautiful graphics with a rich set of functions to build and format any kind of graphs and the plot() function family one that helps us build those.
The plot() function in R isn’t a single defined function but a placeholder for a family of related functions. The exact function being called will depend upon the parameters used. At its simplest, plot() function simply plots two vectors against each other.
This gives a simple plot for y = x^2.
The plot() function in R can be customized in multiple ways to create more complex and eye-catching plots as we will see.
Let us now try constructing a few graphs with what we learned so far.
We will begin by generating a sine wave plot. Let x be a sequence vector of values from -pi to pi with 0.1 intervals and y contains the respective sine values of x. Now try plotting y against x.
x=seq(-pi,pi,0.1) y=sin(x) plot(x,y)
Let us now try changing the symbols and colors.
We are now enabling the compiler to select from 3 different symbols and 4 different colors for marking the graph. Let us see how it turned out.
R also allows combining multiple graphs into a single image for our viewing convenience using the par() function. We only need to set the space before calling the plot function in our graph.
#Set a plotting window with one row and two columns. par(mfrow=c(1,2)) plot(x,y,type='l') plot(x,y,pch=c(4,5,6),col=c('red','blue','violet','green'))
A few more graphs using various options from above are illustrated below.
#Set space for 2 rows and 3 columns. par(mfrow=c(2,3)) #Plot out the graphs using various options. plot(x,cos(x),col=c('blue','orange'),type='o',pch=19,lwd=2,cex=1.5) plot(x,x*2,col='red',type='l') plot(x,x^2/3+4.2, col='violet',type='o',lwd=2,lty=1) plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10),type='o',lty=3,col='pink',lwd=4) plot(x<-seq(1,10,0.5),50*x/(x+2),col=c('green','dark green'),type='h') plot(x,log(x),col='orange',type='s')
The resulting graph looks like this.
Graphs look more complete when there are notes and information that explain them. These include a title for the chart and axes, a legend of the graph. Sometimes even labeling the data points will be necessary. Let us look at how we add these to the graphs in R.
Let us look at examples illustrating these.
#Displaying the title with color plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10),type='o',lty=3, col='pink',lwd=4,main="This is a graph",col.main='blue')
#Same graph with xlabel and ylabel added. > plot(c(1,3,5,7,9,11),c(2,7,5,10,8,10),type='o',lt=3,col='pink',lwd=4,main="This is a graph",col.main='blue',xlab="Time",ylab="Performance")
Let us add a label to each of the data points in the graph using a text attribute.
labelset <-c('one','three','five','seven','nine','eleven') x1<- c(1,3,5,7,9,11) y1 <- c(2,7,5,10,8,10) plot(x1,y1,type='o',lty=3,col='pink',lwd=4,main="This is a graph",col.main='blue',xlab="Time",ylab="Performance") text(x1+0.5,y1,labelset,col='red')
Finally, let us add a legend to the above graph using the legend() function.
The position can be specified by either x and y coordinates or using a position like ‘topleft’ or ‘bottomright’. Inset refers to moving the legend box a little to the inside of the graph. The resulting graph now has a legend.
R also allows two graphs to be displayed on top of each other instead of creating a new window for every graph. This is done by calling a lines() function for the second graph rather than plot() again. These are most useful when performing comparisons of metrics or among different sets of values. Let us look at an example.
x=seq(2,10,0.1) y1=x^2 y2=x^3 plot(x,y1,type='l',col='red') lines(x,y2,col='green') legend('bottomright',inset=0.05,c("Squares","Cubes"),lty=1,col=c("red","green"),title="Graph type")
Straight lines can be added to an existing plot using the simple
abline() function. The abline() function takes 4 arguments, a, b, h, and v. The variables a and b represent the slope and intercept. H represents the y points for horizontal lines and v represents the x points for vertical lines.
Let us look at an example to make this clear. Try executing these three statements after building the above graph for squares and cubes.
abline(a=4,b=5,col='blue') abline(h=c(4,6,8),col="dark green",lty=2) abline(v=c(4,6,8),col="dark green",lty=2)
The first blue line is built with the slope and intercept specified. The next sets of three horizontal and vertical lines are drawn at the specified x and y values in the dotted line style as mentioned by lty=2.
This covers the basics of plot function in R. When combined with other packages like ggplot2, R builds the most presentable and dynamic graphics as we will see in further tutorials.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up
Click below to sign up and get $100 of credit to try our products over 60 days!