## Introduction

In this chapter, you will learn how to create and customize various types of plots in R using the base R `plot()`

function. The `plot()`

function is versatile and can be used to create a wide range of plots, including scatter plots, line plots, and more. Understanding how to use this function effectively is essential for data visualization in R.

## Basic Plotting with plot()

The `plot()`

function can be used to create simple plots quickly. The type of plot created depends on the type of data and the parameters specified.

### Scatter Plot

A scatter plot displays points representing the values of two variables.

**Example**:

```
# Creating a scatter plot
x <- rnorm(50)
y <- rnorm(50)
plot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
```

### Line Plot

A line plot connects points representing the values of a variable over time or another continuous variable.

**Example**:

```
# Creating a line plot
x <- 1:10
y <- x^2
plot(x, y, type = "l", main = "Line Plot", xlab = "X-axis", ylab = "Y-axis")
```

### Scatter Plot with Points and Lines

You can combine points and lines in a single plot.

**Example**:

```
# Creating a scatter plot with points and lines
plot(x, y, type = "b", main = "Scatter Plot with Points and Lines", xlab = "X-axis", ylab = "Y-axis")
```

## Customizing Plots

You can customize plots by adding colors, labels, titles, and other graphical parameters.

### Adding Colors

Use the `col`

parameter to specify colors for the points or lines.

**Example**:

```
# Customizing the plot with colors
plot(x, y, type = "b", col = "blue", main = "Customized Plot", xlab = "X-axis", ylab = "Y-axis")
```

### Adding Titles and Axis Labels

Add main titles, axis labels, and subtitles using the `main`

, `xlab`

, `ylab`

, and `sub`

parameters.

**Example**:

```
# Adding titles and axis labels
plot(x, y, type = "b", col = "blue", main = "Customized Plot", xlab = "X-axis", ylab = "Y-axis", sub = "Subtitle")
```

### Adding Legends

Use the `legend()`

function to add a legend to the plot.

**Example**:

```
# Adding a legend
plot(x, y, type = "b", col = "blue", main = "Plot with Legend", xlab = "X-axis", ylab = "Y-axis")
legend("topright", legend = "y = x^2", col = "blue", pch = 1, lty = 1)
```

### Multiple Plots

You can add multiple plots to a single graph by using the `par()`

function with the `mfrow`

or `mfcol`

parameters.

**Example**:

```
# Creating multiple plots
par(mfrow = c(2, 2)) # 2 rows, 2 columns
plot(x, y, main = "Plot 1")
plot(x, y^2, main = "Plot 2")
plot(x, y^3, main = "Plot 3")
plot(x, y^4, main = "Plot 4")
par(mfrow = c(1, 1)) # Reset to 1 plot
```

### Customizing Points and Lines

You can customize the appearance of points and lines using the `pch`

(plotting character) and `lty`

(line type) parameters.

**Example**:

```
# Customizing points and lines
plot(x, y, type = "b", pch = 19, lty = 2, col = "blue", main = "Customized Points and Lines", xlab = "X-axis", ylab = "Y-axis")
```

## Example Program Using plot()

Here is an example program that demonstrates the creation and customization of various plots in R using the base `plot()`

function.

```
# R Program to Demonstrate Various Plots
# Data for the plots
x <- 1:10
y <- x^2
# Basic scatter plot
plot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
# Line plot
plot(x, y, type = "l", main = "Line Plot", xlab = "X-axis", ylab = "Y-axis")
# Scatter plot with points and lines
plot(x, y, type = "b", main = "Scatter Plot with Points and Lines", xlab = "X-axis", ylab = "Y-axis")
# Customized plot with colors
plot(x, y, type = "b", col = "blue", main = "Customized Plot", xlab = "X-axis", ylab = "Y-axis")
# Adding titles and axis labels
plot(x, y, type = "b", col = "blue", main = "Customized Plot", xlab = "X-axis", ylab = "Y-axis", sub = "Subtitle")
# Adding a legend
plot(x, y, type = "b", col = "blue", main = "Plot with Legend", xlab = "X-axis", ylab = "Y-axis")
legend("topright", legend = "y = x^2", col = "blue", pch = 1, lty = 1)
# Creating multiple plots
par(mfrow = c(2, 2)) # 2 rows, 2 columns
plot(x, y, main = "Plot 1")
plot(x, y^2, main = "Plot 2")
plot(x, y^3, main = "Plot 3")
plot(x, y^4, main = "Plot 4")
par(mfrow = c(1, 1)) # Reset to 1 plot
# Customized points and lines
plot(x, y, type = "b", pch = 19, lty = 2, col = "blue", main = "Customized Points and Lines", xlab = "X-axis", ylab = "Y-axis")
```

## Conclusion

In this chapter, you learned how to create and customize various types of plots in R using the base R `plot()`

function. Plots are essential for data visualization, helping you to understand patterns, trends, and relationships in your data. By mastering the `plot()`

function, you can effectively communicate your data insights through visualizations.