Plotting of Data using Generic plots in R Programming - plot() Function
Last Updated :
19 Dec, 2023
In this article, we will discuss how we plot data using Generic plots in R Programming Language using plot() Function.
plot function
plot() function in R Programming Language is defined as a generic function for plotting. It can be used to create basic graphs of a different type.
Syntax: plot(x, y, type)
Parameters
- x and y: coordinates of points to plot
- type: the type of graph to create
Returns: different type of plots
Draw Points using plot() Function in R
R
Output:
plot() Function in RDraw Multiple Points
R
plot(c(1, 3, 4), c(4, 5 , 8))
Output:
plot() Function in RDraw Sequences of Points
R
Output:
plot() Function in RR program to plot a graph
R
# Values for x and y axis
x <- 1:5
y <- x * x
# Using plot() function with additional settings
plot(x, y, type = "l", col = "blue", lwd = 2, xlab = "X-axis",
ylab = "Y-axis", main = "Quadratic Function")
# Add grid lines
grid()
# Add points to highlight data
points(x, y, col = "red", pch = 16)
# Add a legend
legend("topleft", legend = "y = x^2", col = "blue", lty = 1, lwd = 2, pch = 16)
Output:
plot() Function in R
In this code we creates a line plot with labeled axes, a title, grid lines, and additional points.
col
: Specifies the color of the line,lwd
: Sets the line width,xlab
and ylab
: Label the x-axis and y-axis, respectively.main
: Adds a title to the plot,grid()
: Adds grid lines to the plot,points()
: Adds points to the plot to highlight the data.legend()
: Adds a legend to the plot.
R program to Customize graph
R
# Creating x and y-values
x <- 1:5
y <- x * x
# Using plot function with additional settings
plot(x, y, type = "b", col = "blue", pch = 16, lty = 2,
main = "Quadratic Function", xlab = "X-axis", ylab = "Y-axis")
# Add grid lines
grid()
# Add points to highlight data
points(x, y, col = "red", pch = 16)
# Add a legend
legend("topleft", legend = "y = x^2", col = "blue", pch = 16, lty = 2)
# Adding a title
title(main = "Quadratic Function", sub = "y = x^2", col.main = "blue",
col.sub = "red", font.main = 4, cex.main = 1.2, cex.sub = 0.8)
Output:
plot() Function in R
In this code we creates a quadratic function plot with blue points connected by dashed lines. It includes a title, axis labels, grid lines, red-highlighted data points, and a legend indicating the equation 2y=x2. The main title and subtitle have custom colors and font styles for improved visualization.
Multiple Plots In R
R
# Create data for multiple plots
x <- 1:5
y1 <- x * x
y2 <- 2 * x
y3 <- x^2 - 3
# Set up a 2x2 grid for plots
par(mfrow = c(2, 2))
# Plot the first graph
plot(x, y1, type = "b", col = "blue", pch = 16,
main = "Plot 1", xlab = "X-axis", ylab = "Y-axis")
# Plot the second graph
plot(x, y2, type = "o", col = "green", pch = 17,
main = "Plot 2", xlab = "X-axis", ylab = "Y-axis")
# Plot the third graph
plot(x, y3, type = "l", col = "red", lty = 2,
main = "Plot 3", xlab = "X-axis", ylab = "Y-axis")
# Reset the graphical parameters to default
par(mfrow = c(1, 1))
Output:
plot() Function in R
par(mfrow = c(2, 2))
is like setting up a grid of 2 rows and 2 columns for your plots. This means we can create four plots, and they will be arranged in a 2x2 grid.
- Each time we use the
plot
function after setting up the grid, it adds a new plot to one of the grid positions. Each plot can have different data and visual styles. - This ensures that any future plots you make won't be constrained to the grid; they'll be displayed as standalone plots.
Overlaying Graphs using plot function
R
# Create data for multiple plots
x <- 1:5
y1 <- x * x
y2 <- 2 * x
y3 <- x^2 - 3
# Plot the first graph
plot(x, y1, type = "b", col = "blue", pch = 16, main = "Overlaying Graphs",
xlab = "X-axis", ylab = "Y-axis")
# Overlay the second graph
points(x, y2, col = "green", pch = 17)
# Overlay the third graph
lines(x, y3, col = "red", lty = 2)
# Add a legend
legend("topleft", legend = c("y = x^2", "y = 2x", "y = x^2 - 3"),
col = c("blue", "green", "red"), pch = c(16, 17, NA), lty = c(1, 1, 2))
Output:
plot() Function in R
In this example the plot
function is used to create the first graph. the points
function overlays points from the second graph on the existing plot.
The lines
function overlays a line from the third graph on the existing plot. legend
adds a legend to distinguish between different datasets.
Similar Reads
Plotting Graphs using Two Dimensional List in R Programming List is a type of an object in R programming. Lists can contain heterogeneous elements like strings, numeric, matrices, or even lists. A list is a generic vector containing other objects. Two-dimensional list can be created in R programming by creating more lists in a list or simply, we can say nest
2 min read
Printing plots generated in a function using R Markdown in RStudio In this article, we will explore how to generate and print the plots created within the function using R Markdown in RStudio. R Markdown is a powerful tool for creating dynamic documents that integrate code, results, and narrative text. We will demonstrate how to create the plot within the R functio
3 min read
Create a Plot Matrix of Scatterplots in R Programming - pairs() Function pairs() function in R language is used to return a plot matrix, consisting of scatter plots corresponding to each data frame. R - Create Plot Matrix of Scatterplots Syntax: pairs(data) Parameters: data: It is defined as  value of pairs Plot. Returns: Color, Labels, Panels, and by Group in pairs plo
2 min read
Adding axis to a Plot in R programming - axis () Function axis() function in R Language is to add axis to a plot. It takes side of the plot where axis is to be drawn as argument. Syntax: axis(side, at=NULL, labels=TRUE) Parameters: side: It defines the side of the plot the axis is to be drawn on possible values such as below, left, above, and right. at: Po
2 min read
Adding Text to Plots in R programming - text() and mtext () Function Text is defined as a way to describe data related to graphical representation. They work as labels to any pictorial or graphical representation.1. text() Function in Rtext() Function in R Programming Language is used to draw text elements to plots in Base R.Syntax: text(x, y, labels)Parameters:Â x an
3 min read