What Is plot() Function

Q

How to use the plot() function to generate graphics?

✍: FYIcenter.com

A

plot() function allows you to generate a graph representing a given sequence of x-y points. plot() function is provided in the built-in "graphics" package.

plot() function takes the following positional arguments:

• p1 - x-coordinates of points. p1 could also be a data structures with 2 vectors/lists, which represents both x- and y-coordinates, or a function.
• p2 - optional, y-coordinates of points. If p1 is a function, p2 will be the starting value of the independent variable.
• p3 - optional, the ending value of the independent variable, if p1 is a function.

plot() function also takes 5 optional named arguments:

• type - type of graph to be generated, "p" for points, "l" for lines, "b" for both, "h" for histogram, "s" for stairs, etc.
• main - optional, text for the main title.
• sub - optional, text for the sub-title.
• xlab - optional, text for the x-axis label.
• ylab - optional, text for the y-axis label.
• asp - optional, aspect ratio of y- over x-axis.

Examples calling plot() function:

```# coordinates specified as 2 vectors
plot( c(1,2,3,4), c(31, 22, 13, 44) )

# coordinates specified as a data frame
plot( c(1,2,3,4), c(31, 22, 13, 44) )

# coordinates specified as a function
plot(sin, -pi, 2*pi)
```

2023-05-09, 342🔥, 0💬