Visualizing Data in R with Default Package:
Scatter Plot

Asst. Prof. Dr. Somsak Chanaim

International College of Digital Innovation, CMU

September 10, 2026

Examples of scatter plot

Load library

library(gapminder)
library(dplyr)
library(RColorBrewer)

A scatter plot is a type of data visualization that is used to display the relationship between two continuous variables.

It is often employed in statistics and data analysis to identify patterns, trends, and correlations between the variables.

In a scatter plot, each data point is represented by a dot, and the position of the dot on the graph corresponds to the values of the two variables.

The horizontal axis (x-axis) typically represents one variable, while the vertical axis (y-axis) represents the other variable.

Each point on the graph represents a pair of values from the two variables.

By examining the distribution of points, you can gain insights into the nature of the relationship between the variables.

Scatter plots are particularly useful for identifying trends, outliers, clusters, or any patterns that may exist in the data.

How to draw the scatter plot with R using plot() function?

To create a scatter plot in R using the plot() function, we need to provide the vectors or data frames representing the variables you want to plot on the x-axis and y-axis.

example:

Changing color, symbol, and size

In this example

  • x and y are vectors representing the data for the x-axis and y-axis, respectively.

  • main, xlab, and ylab are optional parameters specifying the main title, x-axis label, and y-axis label, respectively.

  • pch sets the type of plotting symbol (in this case, a solid dot).

  • col sets the color of the dots.

You can customize these parameters based on your specific data and preferences. If you have a data frame, you can use column names to access variables. For example:

This example assumes a simple scatter plot, but you can use additional parameters and functions to add more features, such as regression lines, labels, and more, depending on your analysis requirements.

pch

In R, the pch (plot character) argument is used in the base plot function to specify the type of plotting symbol or point character to be used in the scatter plot.

The pch parameter allows you to customize the appearance of the points on the plot.

Some common values for the pch argument:

  • pch = 1: Hollow circle
  • pch = 2: Filled circle
  • pch = 3: Cross
  • pch = 4: X
  • pch = 5: Diamond
  • pch = 6: Square
  • pch = 7: Triangle point up
  • pch = 8: Triangle point down
  • pch = 9: Solid dot
  • etc.

The pch argument is used to change the plotting symbols for different groups of points.

The legend() function is used to add a legend explaining the meaning of each pch value.

You can experiment with different pch values to customize the appearance of points in your scatter plot based on your preferences.

The important argument for scatter plot

  • bg = Fill color inside the symbol.

  • col = Border color the symbol

Can you see something ?

iris dataset

Example

If speed is greater than 20, assign the color red; otherwise, assign blue.

If speed is less than 10 and distance is less than 40, assign the color red; otherwise, assign blue.

Add regression line to the scatter plot

Linear regression

Add smooth line to the scatter plot

Add linear and non linear

Gapminer data from package gapminder

scatter plot between lifeExp vs gdpPercap

The scatter plot between lifeExp vs gdpPercap select the data only year 2007

The scatter plot between lifeExp vs gdpPercap select the data only year 2007

  • add linear regression blue line

  • pch is red circle.

RColorBrewer

select “Accent”

Add legend

Change background and add grid line

Basic way

Another way

Exercise

Exercise 1: Fundamental Scatter Plot Architecture

Complete the script to map two numeric vectors from the mtcars dataset onto a bivariate scatter plot and assign a descriptive main title.

Target output

Complete the code

plot( = mtcars$wt,
= mtcars$mpg,
main = "Scatter Plot of Weight vs. MPG",
xlab = "Weight",
ylab = "Miles Per Gallon")

Exercise 2: Point Aesthetic and Scale Customization

Complete the script to render a bivariate scatter plot, map point colors dynamically based on grouping levels, and expand the physical symbol scales.

Target output

Complete the code

plot(x = mtcars$wt,
y = mtcars$mpg,
col = mtcars$cyl,
= 19,
= 1.5,
xlab = "Weight",
ylab = "Miles Per Gallon",
main = "Scatter Plot with Customized Points")

legend("topright",
legend = unique(mtcars$cyl),
col = unique(mtcars$cyl),
pch = 19,
title = "Number of Cylinders")

Exercise 3: Linear Regression Fitting and Line Overlay

Complete the script to map the bivariate coordinates, fit a linear regression model, and superimpose the estimated trendline onto the active canvas.

Target output

Complete the code

plot(x = mtcars$wt,
y = mtcars$mpg,
xlab = "Weight",
ylab = "Miles Per Gallon",
main = "Scatter Plot with Regression Line")

abline((mpg ~ wt, data = mtcars), col = "red")

Exercise 4: Graphical Coordinate Text Annotation

Complete the script to map the bivariate coordinates and overlay textual identifier labels at specific discrete observation points.

Target output

Complete the code

plot(x = mtcars$wt,
y = mtcars$mpg,
xlab = "Weight",
ylab = "Miles Per Gallon",
main = "Scatter Plot with Annotations")

(x = mtcars$wt[c(1, 2, 3)],
y = mtcars$mpg[c(1, 2, 3)],
= rownames(mtcars)[c(1, 2, 3)],
pos = 4,
cex = 0.8,
col = "blue")

Exercise 5: Group-Specific Symbol and Color Mapping

Complete the script to render a multivariate scatter plot, map point aesthetics dynamically, and position an explanatory classification legend.

Target output

Complete the code

plot(x = mtcars$wt, y = mtcars$mpg,
col = mtcars$cyl,
= mtcars$cyl,
xlab = "Weight",
ylab = "Miles Per Gallon",
main = "Scatter Plot with Multiple Groups")

("topright",
legend = unique(mtcars$cyl),
col = unique(mtcars$cyl),
= unique(mtcars$cyl),
title = "Number of Cylinders")