R Func Plot
# R plot() Function - Drawing Scatterplots
[ R Language Examples](#)
The R plot() function is the most basic plotting function, used to create scatterplots, line charts, and various other types of graphics.
plot() is the entry point to R's graphics system, and almost all visualizations start with it.
The syntax format of the plot() function is as follows:
plot(x, y, type = "p", main = "", xlab = "", ylab = "", col = "black")
**Parameter Description:**
* **x, y** Coordinates of the data points.
* **type** Graph type: "p" (points), "l" (lines), "b" (both points and lines), "o" (overplotted).
* **main** Chart title.
* **xlab, ylab** x-axis and y-axis labels.
* **col** Color.
## Example
# Create Data
height <-c(160, 165, 170, 175, 180, 158, 172, 168, 178, 182)
weight <-c(55, 60, 65, 70, 80, 52, 68, 62, 75, 85)
# Draw Scatter Plot
plot(height, weight,
main ="Relationship Between Height and Weight",
xlab ="Height (cm)",
ylab ="Weight (kg)",
col="blue",
pch =16)# pch=16 Solid Circles
# Add Trend Line
abline(lm(weight ~ height), col="red", lwd =2)
Executing the above code will display a scatterplot and a red trend line.
plot() can also be directly applied to data frames to automatically generate a scatterplot matrix of the variables:
## Example
# View Relationships Between Variables in the iris Dataset
print("iris Scatter Plot Matrix:")
plot(iris[, 1:4],
col=iris$Species,
main ="Iris Dataset Scatter Plot Matrix")
Executing the above code will generate a 4x4 scatterplot matrix, where points of different colors represent different iris species.
[ R Language Examples](#)
YouTip