# R Plot

The plot() function in R is a versatile function for creating a variety of plots.
Syntax plot(x, y, ...)

### Example

``````x <- c(5,6,7,8,9)
y <- c(10,11,12,13,14)

plot(x,y)``````

Above example is based on two vector points.
We can also use 1 point in a plot() function.
and we can use numeric values instead of a vectors.

### Example

``plot(1)``
If we need to plot from a sequence of numbers you can keep the values from start to end in this format x:y

### Example

``````x <- 1
y <- 5
plot(x:y) # plot(1:5)``````

## Attributes in a plot

There are various attributes in a plot() function to describe and apply the values given.

Attribute

Description

Example

Main Title (main)

Main title of the plot.

plot(x, y, main = "Scatter Plot")

Axis Labels (xlab and ylab)

1. xlab - Label for the x-axis.
2. ylab - Label for the y-axis.

plot(x, y, xlab = "X-axis", ylab = "Y-axis")

Plot Type (type)

Type of plot (e.g., "p" for points, "l" for lines).

plot(x, y, type = "p")

Point Characteristics (col, pch, cex, lty)

1. col - Point color.
2. pch - Point shape.
3. cex - Point size.
4. lty - Line type.

plot(x, y, col = "blue", pch = 16, cex = 1.5, lty = 2)

Plotting Multiple Sets of Data

Use additional arguments like points() or lines() to overlay multiple sets of data

plot(x1, y1, col = "red"); points(x2, y2, col = "blue", pch = 2)

Axes (xlim and ylim)

1. xlim - Limits for the x-axis.
2. ylim - Limits for the y-axis.

plot(x, y, xlim = c(0, 10), ylim = c(-2, 2))

Grid (grid)

Adds a grid to the plot.

plot(x, y, grid = TRUE)

plot(x, y); abline(h = 0, v = 0, col = "red", lty = 2)

### Example

``````# Sample data
set.seed(123)
x <- rnorm(50)
y <- 2 * x + rnorm(50)

# Create a scatter plot with customization
plot(
x, y,
main = "Scatter Plot with Customization",
xlab = "X-axis",
ylab = "Y-axis",
col = "blue",
pch = 16,
cex = 1.5,
xlim = c(-2, 2),
ylim = c(-4, 4),
grid = TRUE
)

abline(lm(y ~ x), col = "red", lty = 2)

# Add points with different color and shape
points(x[25:30], y[25:30], col = "green", pch = 3)

legend("topright", # Specify the position of the legend
legend = c("Data", "Regression Line", "Additional Points"), # Legend text
col = c("blue", "red", "green"), # Line colors corresponding to each distribution
pch = c(16, NA, 3), # Line colors corresponding to each distribution
lty = c(NA, 2, NA), # Line type
cex = 0.8 # Point size)``````

The legend function is used to add a legend to the plot.
Inside legend function we have
The position of the legend, legend text and col specifies the line colors.

## Types of plot

There are different types of plot. Here are the list

Type Argument

Description

Example

p

Scatter Plot

plot(x, y, type = "p")

l

Line Plot

plot(x, y, type = "l")

b

Both Points and Lines

plot(x, y, type = "b")

s

Step Plot

plot(x, y, type = "s")

h

Histogram

plot(x, y, type = "h")

S

Staircase Plot

plot(x, y, type = "S")

e

Error Bars

plot(x, y, type = "e")

box

Box Plot

plot(x, type = "box")

contour

Contour Plot

plot(x, y, type = "contour")

heatmap

Heatmap (with matrix data)

plot(matrix_data, type = "h")

# Quick Recap - Topics Covered

R Plot
Attributes in a plot
Types of Plot

## Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.

### Example js

Example 1 Example 2 Example 3 Example 4 Example 5

FEEDBACK