Thursday, January 30, 2025
HomeMathematicsHow do you create scatter plots in R

How do you create scatter plots in R

Creating Scatter Plots in R

In R, you can create scatter plots using the plot() function from base R or the ggplot2 package for more advanced visualizations.


1️⃣ Using Base R (plot())

The simplest way to create a scatter plot is with the plot() function.

Example: Basic Scatter Plot

# Sample Data
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)

# Create Scatter Plot
plot(x, y,
main = "Basic Scatter Plot in R",
xlab = "X-axis Label",
ylab = "Y-axis Label",
col = "blue",
pch = 19) # pch controls point shape

🔹 Customization Options:

  • main → Title of the plot
  • xlab, ylab → Axis labels
  • col → Color of points
  • pch → Point style (e.g., 19 for solid circles, 16 for dots)

2️⃣ Adding a Regression Line

To add a trend line, use lm() (linear model) with abline().

model <- lm(y ~ x) # Create linear regression model
plot(x, y, pch = 19, col = "blue")
abline(model, col = "red", lwd = 2) # Add regression line

3️⃣ Using ggplot2 for Advanced Scatter Plots

ggplot2 offers a more customizable approach for scatter plots.

Installing & Loading ggplot2

install.packages("ggplot2") # Install (only needed once)
library(ggplot2) # Load the package

Example: Basic Scatter Plot with ggplot2

# Sample Data (data frame)
df <- data.frame(x = x, y = y)

# Scatter Plot with ggplot2
ggplot(df, aes(x = x, y = y)) +
geom_point(color = "blue", size = 3) +
labs(title = "Scatter Plot with ggplot2",
x = "X-axis Label",
y = "Y-axis Label") +
theme_minimal()


4️⃣ Adding a Regression Line in ggplot2

ggplot(df, aes(x = x, y = y)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", color = "red", se = FALSE) + # Linear trend line
labs(title = "Scatter Plot with Trend Line",
x = "X-axis",
y = "Y-axis") +
theme_minimal()

🔹 geom_smooth(method = "lm") adds a linear regression line.
🔹 se = FALSE removes the confidence interval shading.


5️⃣ Scatter Plot with Groups (Colored by Category)

# Sample Data with Categories
df <- data.frame(
x = rnorm(50),
y = rnorm(50),
category = sample(c("A", "B"), 50, replace = TRUE)
)

# Scatter Plot with Different Colors by Category
ggplot(df, aes(x = x, y = y, color = category)) +
geom_point(size = 3) +
labs(title = "Scatter Plot Colored by Category") +
theme_minimal()


📌 Summary

Method Function Used Best For
Base R plot() Simple scatter plots
Base R + Regression plot() + abline() Scatter plots with trend lines
ggplot2 ggplot() + geom_point() Customizable, professional-looking plots
ggplot2 + Regression ggplot() + geom_smooth(method = "lm") Scatter plots with linear regression

Would you like additional customizations or help with a specific dataset?

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x