0
0
R-programmingHow-ToBeginner ยท 3 min read

How to Create Scatter Plot with ggplot2 in R

To create a scatter plot in ggplot2, use ggplot() with aes() to map your x and y variables, then add geom_point() to plot points. For example, ggplot(data, aes(x, y)) + geom_point() creates a basic scatter plot.
๐Ÿ“

Syntax

The basic syntax for a scatter plot in ggplot2 is:

  • ggplot(data, aes(x, y)): sets the data and maps the x and y variables.
  • geom_point(): adds points to create the scatter plot.
r
ggplot(data, aes(x = x_variable, y = y_variable)) +
  geom_point()
๐Ÿ’ป

Example

This example shows how to create a scatter plot using the built-in mtcars dataset, plotting wt (weight) on the x-axis and mpg (miles per gallon) on the y-axis.

r
library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Scatter Plot of Weight vs MPG",
       x = "Weight (1000 lbs)",
       y = "Miles per Gallon")
Output
[A scatter plot appears showing points scattered with weight on the x-axis and mpg on the y-axis]
โš ๏ธ

Common Pitfalls

Common mistakes when creating scatter plots with ggplot2 include:

  • Not loading the ggplot2 library before using its functions.
  • Forgetting to map variables inside aes(), which causes no points to appear.
  • Using incorrect variable names or data frames.

Example of a wrong and right approach:

r
# Wrong: Missing aes mapping
library(ggplot2)
ggplot(mtcars) +
  geom_point(aes(x = wt, y = mpg))  # This will work but is less common

# Right: Correct aes mapping

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()
๐Ÿ“Š

Quick Reference

FunctionPurpose
ggplot(data, aes(x, y))Initialize plot with data and variable mapping
geom_point()Add points to create scatter plot
labs(title, x, y)Add labels and title
theme()Customize plot appearance
โœ…

Key Takeaways

Use ggplot(data, aes(x, y)) + geom_point() to create a scatter plot.
Always map variables inside aes() for ggplot2 to recognize them.
Load the ggplot2 library before plotting.
Add labels with labs() to make your plot clear.
Check variable names and data frame to avoid errors.