Scatter plots are essential tools for visualizing relationships between two continuous variables in R. They provide insights into data patterns, correlations, and outliers.
R offers multiple ways to create scatter plots. The most straightforward method uses the plot()
function from base R graphics.
# Basic scatter plot
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 5, 4, 5)
plot(x, y, main="Basic Scatter Plot", xlab="X-axis", ylab="Y-axis")
Enhance your scatter plots with various customization options:
pch
parametercex
col
# Customized scatter plot
plot(x, y, main="Customized Scatter Plot",
xlab="X-axis", ylab="Y-axis",
pch=16, cex=1.5, col="blue")
For more advanced and aesthetically pleasing scatter plots, consider using the ggplot2 package. It offers greater flexibility and ease of customization.
# Install and load ggplot2
install.packages("ggplot2")
library(ggplot2)
# Create a data frame
df <- data.frame(x = x, y = y)
# Create scatter plot with ggplot2
ggplot(df, aes(x = x, y = y)) +
geom_point() +
labs(title = "Scatter Plot with ggplot2",
x = "X-axis", y = "Y-axis")
Enhance your scatter plots by adding trend lines to visualize relationships more clearly. Use geom_smooth()
in ggplot2 for this purpose.
ggplot(df, aes(x = x, y = y)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Scatter Plot with Trend Line",
x = "X-axis", y = "Y-axis")
To further enhance your data visualization skills in R, explore these related topics:
By mastering scatter plots in R, you'll be well-equipped to explore and present relationships in your data effectively. Practice with different datasets to gain proficiency in this essential data visualization technique.