R Plot
R provides powerful tools for data visualization, and the plot()
function is one of the most widely used functions for creating basic plots. It can generate scatter plots, line plots, and more.
Key Topics
Basic Plot
The plot()
function can be used to create a simple scatter plot.
# Creating a basic plot
x <- c(1, 2, 3, 4, 5)
y <- c(5, 4, 3, 2, 1)
plot(x, y, main = "Basic Scatter Plot", xlab = "X-axis", ylab = "Y-axis")
Output:
A scatter plot with points at coordinates (1, 5), (2, 4), (3, 3), (4, 2), and (5, 1).
Code Explanation: The plot()
function creates a scatter plot with the x
and y
values. The main
, xlab
, and ylab
parameters are used to add a title and axis labels.
Customizing Plots
You can customize plots by changing colors, point shapes, and line types.
# Customizing the plot
plot(x, y, col = "blue", pch = 16, type = "b", lty = 2,
main = "Customized Plot", xlab = "X-axis", ylab = "Y-axis")
Output:
A customized plot with blue points, connected by dashed lines.
Code Explanation: The col
parameter sets the color to blue, pch
changes the point shape, type
specifies both points and lines, and lty
sets the line type to dashed.
Key Takeaways
- The
plot()
function is used to create basic and customized plots in R. - Customizations include setting colors, point shapes, and line types.
- Visualizations are essential for data analysis and presentation.