Browse By

R Language –Presentation of Data : Online Course

R Plot Graph

  • Function plot() is the main tool for 2D plotting in R.
  • If we have two vectors x, y of equal length, function plot(x,y) allows us to get the graph of y(x) dependence.
  • The function plot(x,y) takes parameters for specifying points in the diagram.
    1. Parameter 1 specifies points on the x-axis.
    2. Parameter 2 specifies points on the y-axis.

If only one variable is given, we will get a graph for which set vector provide the ordinate values and the points ordinal numbers are abscissas.

Syntax of the Plot Function in R

The generic syntax for a plot in Rstudio is:

Plot(x,y,…)

And its complete syntax is:

plot(x, y, type, main, sub,  xlab, ylab)

Some arguments of the plot() function:

Following arguments in details.

S.NoParameterDescription
1.vIt is a vector which contains the numeric values. x is specifies points on the x-axis. y is specifies points on the y-axis.
2.typechange the plot type. It accepts the following strings and has the given effect.”p” – points
“l” – lines
“b” – both points and lines
“c” – empty points joined by lines
“o” – overplotted points and lines
“s” and “S” – stair steps
“h” – histogram-like vertical lines
“n” – does not produce any points or lines
3.xlabIt is the label for the x-axis.
4.ylabIt is the label for the y-axis.
5.mainIt is the title of the chart.
6.colIt is used to give the color for both the points and lines
7pchpch is used to change the point shape format.
8cexUse cex=number to change the size of the points
9Legend()Legend takes as input the coordinates, text and the symbols to be interpreted.
10Itylty is the text sequence of four numbers from 1 to 9, for example: lty = “4241”.

Example : Single Point

Input:To draw single point using plot function
plot(2, 3)
Result:

Example : Multiple Point

Input: To draw multiple point using plot function
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
Output:
Example : Sequences of Points
Input: To draw a sequence of points using plot function
plot(1:10)
Output:
Example : Plot Labels and Colors
Input: To draw Plot Labels and Colors using main, xlab, ylab, col parameter
plot(1:10, main=”My Graph”, xlab=”The x-axis”, ylab=”The y axis”, col=”blue”)
Output:
Example : Size and Point Shape
Input: To draw size and point shape using “cex” and “pch” parameter
plot(1:10, main=”My Graph”, xlab=”The x-axis”, ylab=”The y axis”, pch=13, cex = 3)
Output:

The values of the pch parameter ranges from 0 to 25, which means that we can choose up to 26 different types of point shapes: