plot() functions is the base generic function for most of the graphical operations.
1.1.1) Plot two vectors: When two vectors were given as inputs for a plot function, the first vector was considered as x-axis and the second vector was considered as y-axis.They are interpreted as a set of pairs in the (x,y) plane.
plot(c(7,5,6),c(2,3,4))
1.1.2) Plot a single vector:
The members in a vector will be considered as y-axis and ploted accordingly.
plot(c(7,5,6.6,5.5,6))
The general syntax of plot function is :
plot(v, type, col,main,xlab,ylab)
And the parameters are:
*v is a vector containing the numeric values.
*type takes the value
* "p" to draw only the points,
* "l" to draw only the lines and
* "o" to draw both points and lines.
* xlab is the label for x axis.
* ylab is the label for y axis.
* main is the Title of the chart.
* col is used to give colors to both the points and lines.
Example 1:
plot(c(2,3,4,5),c(2,3,2.75,3.25),type="o",xlab="Expenditure",ylab="profit",main="The sales of XYZ Company",col="blue")
Example 2: Draw a sine wave from -3 to 3 using a plot with a sequence of 0.1.
x<-seq(from = -3,to=3,by = 0.1)
plot(x,sin(x),main = "The sine wave",type = "l",xlab="x",ylab="y",col="red")
In the above example, only one entity is presented (sine wave). But, if we want to compare multiple entities in a same graph (say sine and cosine wave), we should use lines() functions along with plot function.
NOTE: You can add more lines using the lines() function. Though there are many options, the two basic arguments to lines() are a vector of x-values and a vector of y-values. These are interpreted as (x,y) pairs representing points to be added to the current graph, with lines connecting the points.
Example 3: Add cos wave to example 2.
plot(x,sin(x),main = "The sine and cosine waves",type = "l",xlab="x",ylab="y",col="red")
lines(x,cos(x),col="blue")
The graph legend is a special text object that is created on the graph. Legend descriptions are necessary to understand the graph.
Example 4: Add a proper legend to example 2
plot(x,sin(x),main = "The sine wave",type = "l",xlab="x",ylab="y",col="red")
lines(x,cos(x),col="blue")
legend("topleft",c("sin(x)","cos(x)"),fill=c("red","blue"))
plot(c(-3,3),c(-1,5),type="n",xlab="xaxis",ylab="yaxis")
The R function abline() can be used to add vertical, horizontal or regression lines to a graph. It’s syntax is:
abline(a = NULL, b = NULL, h = NULL, v = NULL…..)
plot(c(-3,3),c(-1,5),type="n",xlab="PRADEEP",ylab="PANDU") # creates an empty graph
abline(h=4,col="red") # draws a horizontal line at a specified y-axis
plot(c(-3,3),c(-1,5),type="n",xlab="x-axis",ylab="y-axis") # creates an empty graph
abline(v=2,col="red")# draws a verical line at a specified x-axis
The result of the call to the linear-regression function lm() is a class instance containing the slope and intercept of the fitted line.
p <- c(1,2,3,5,6,4,3)
q <- c(3,4,5,3,3,5,6)
plot(p,q)
lmout <- lm(q ~ p)
print(lmout)
##
## Call:
## lm(formula = q ~ p)
##
## Coefficients:
## (Intercept) p
## 4.6129 -0.1371
abline(lmout)
We can plot multiple graphs at same time using following commands
On Linux systems, call X11(). On a Mac, call macintosh(). On Windows, call windows()
a<-c(3,2,4,6,7)
b<-c(5,6,7,8,9)
d<-c(34,65,234,54)
e<-c(23,654,754,234)
plot(a,b)
windows()
plot(d,e)
The points() function adds a set of (x,y) points, with labels for each, to the currently displayed graph.
1.8.1) Example 1: Use a built in dataset “faithful” and draw a graph between “eruptions” and “waiting” colomns of the dataset. Then mark all eruptions less than 3 as red. Let’s consider a built in dataset “faithful”. It has 2 variables “eruptions” and “waiting”. These observations are ploted using plot() function. The first 6 entries of “faithful” dataset is displayed using head function.
head(faithful)
## eruptions waiting
## 1 3.600 79
## 2 1.800 54
## 3 3.333 74
## 4 2.283 62
## 5 4.533 85
## 6 2.883 55
print(str(faithful))
## 'data.frame': 272 obs. of 2 variables:
## $ eruptions: num 3.6 1.8 3.33 2.28 4.53 ...
## $ waiting : num 79 54 74 62 85 55 88 85 51 85 ...
## NULL
plot(faithful)
plot(faithful)
points(faithful[faithful$eruptions<3,], col="red",pch=19)
a<-c(3,2,4,6,7)
b<-c(5,6,7,8,9)
plot(a,b)
d<-c(6.3,5.6)
e<-c(5.5,6.5)
points(d,e,pch=18,col="blue")
There are 2 methods to add text inside the graph
text() function Locator() function
a<-c(1,2,3,4,5)
b<-c(5,6,5.5,8,9)
plot(a,b,type="o")
text(3,5.5,"There is a fall",col="red")
2) locator() function: Placing text exactly where you wish can be difficult using text function. Use locator function and click on the graph to get the desired location. The function returns the “x” and “y” coordinates of your click point.
a<-c(1,2,3,4,5)
b<-c(5,6,5.5,8,9)
plot(a,b,type="o")
locator(1)