R base graphics is the built-in plotting system that comes with R. It provides a powerful and flexible set of tools for creating a wide range of statistical graphics and data visualizations.
To create a basic plot in R, you can use the plot()
function. This versatile function can handle various data types and automatically chooses an appropriate plot type.
# Create a simple scatter plot
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
plot(x, y, main="Simple Scatter Plot", xlab="X-axis", ylab="Y-axis")
R base graphics offers numerous parameters to customize your plots. You can modify colors, line types, point shapes, and more.
# Customized line plot
plot(x, y, type="l", col="blue", lwd=2, main="Customized Line Plot")
points(x, y, pch=16, col="red")
R base graphics supports various plot types, including:
You can enhance your plots by adding additional elements such as text, legends, and lines.
plot(x, y, main="Enhanced Plot")
text(3, 6, "Important Point", col="red")
legend("topleft", legend=c("Data", "Trend"), col=c("black", "blue"), lty=1:2)
abline(lm(y ~ x), col="blue", lty=2)
R base graphics allows you to create multiple plots in a single graphical device using the par()
function.
par(mfrow=c(2,2))
plot(x, y, main="Plot 1")
plot(y, x, main="Plot 2")
hist(x, main="Plot 3")
boxplot(y, main="Plot 4")
While R base graphics is powerful, it's worth exploring other graphing systems in R, such as ggplot2 or Plotly, for more advanced or interactive visualizations.
R base graphics provides a solid foundation for creating various types of plots in R. Its simplicity and flexibility make it an excellent starting point for data visualization in R, especially for beginners or quick exploratory data analysis.