Scatter Plots - R Base Graphs - Easy Guides - Wiki (2024)

  • Pleleminary tasks
  • R base scatter plot: plot()
  • Enhanced scatter plots: car::scatterplot()
  • 3D scatter plots
  • Summary
  • Related articles
  • See also
  • Infos

Previously, we described the essentials of R programming and provided quick start guides for importing data into R.


Here, we’ll describe how to make a scatter plot. A scatter plot can be created using the function plot(x, y).The function lm() will be used to fit linear models between y and x. A regression line will be added on the plot using the function abline(), which takes the output of lm() as an argument. You can also add a smoothing line using the function loess().

  1. Launch RStudio as described here: Running RStudio and setting up your working directory

  2. Prepare your data as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files

  3. Import your data into R as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in mtcars data set.

x <- mtcars$wty <- mtcars$mpg# Plot with main and axis titles# Change point shape (pch = 19) and remove frame.plot(x, y, main = "Main title", xlab = "X axis title", ylab = "Y axis title", pch = 19, frame = FALSE)# Add regression lineplot(x, y, main = "Main title", xlab = "X axis title", ylab = "Y axis title", pch = 19, frame = FALSE)abline(lm(y ~ x, data = mtcars), col = "blue")

Scatter Plots - R Base Graphs - Easy Guides - Wiki (1)

# Add loess fitplot(x, y, main = "Main title", xlab = "X axis title", ylab = "Y axis title", pch = 19, frame = FALSE)lines(lowess(x, y), col = "blue")

Scatter Plots - R Base Graphs - Easy Guides - Wiki (2)

The function scatterplot() [in car package] makes enhanced scatter plots, with box plots in the margins, a non-parametric regression smooth, smoothed conditional spread, outlier identification, and a regression line, …

  • Install car package:
install.packages("car")
  • Use scatterplot() function:
library("car")scatterplot(wt ~ mpg, data = mtcars)

Scatter Plots - R Base Graphs - Easy Guides - Wiki (3)

The plot contains:


  • the points
  • the regression line (in green)
  • the smoothed conditional spread (in red dashed line)
  • the non-parametric regression smooth (solid line, red)
# Suppress the smoother and framescatterplot(wt ~ mpg, data = mtcars, smoother = FALSE, grid = FALSE, frame = FALSE)

Scatter Plots - R Base Graphs - Easy Guides - Wiki (4)

# Scatter plot by groups ("cyl")scatterplot(wt ~ mpg | cyl, data = mtcars, smoother = FALSE, grid = FALSE, frame = FALSE)

Scatter Plots - R Base Graphs - Easy Guides - Wiki (5)

It’s also possible to add labels using the following arguments:


  • labels: a vector of point labels
  • id.n, id.cex, id.col: Arguments for labeling points specifying the number, the size and the color of points to be labelled.
# Add labelsscatterplot(wt ~ mpg, data = mtcars, smoother = FALSE, grid = FALSE, frame = FALSE, labels = rownames(mtcars), id.n = nrow(mtcars), id.cex = 0.7, id.col = "steelblue", ellipse = TRUE)

Scatter Plots - R Base Graphs - Easy Guides - Wiki (6)

## Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout Valiant ## 1 2 3 4 5 6 ## Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE ## 7 8 9 10 11 12 ## Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental Chrysler Imperial Fiat 128 ## 13 14 15 16 17 18 ## Honda Civic Toyota Corolla Toyota Corona Dodge Challenger AMC Javelin Camaro Z28 ## 19 20 21 22 23 24 ## Pontiac Firebird Fiat X1-9 Porsche 914-2 Lotus Europa Ford Pantera L Ferrari Dino ## 25 26 27 28 29 30 ## Maserati Bora Volvo 142E ## 31 32

Other arguments can be used such as:


  • log to produce log axes. Allowed values are log = “x”, log = “y” or log = “xy”
  • boxplots: Allowed values are:
    • “x”: a box plot for x is drawn below the plot
    • “y”: a box plot for y is drawn to the left of the plot
    • “xy”: both box plots are drawn
    • “” or FALSE to suppress both box plots.
  • ellipse: if TRUE data-concentration ellipses are plotted.

To plot a 3D scatterplot the function scatterplot3D [in scatterplot3D package can be used].

The following R code plots a 3D scatter plot using iris data set.

head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 5.1 3.5 1.4 0.2 setosa## 2 4.9 3.0 1.4 0.2 setosa## 3 4.7 3.2 1.3 0.2 setosa## 4 4.6 3.1 1.5 0.2 setosa## 5 5.0 3.6 1.4 0.2 setosa## 6 5.4 3.9 1.7 0.4 setosa
# Prepare the data setx <- iris$Sepal.Lengthy <- iris$Sepal.Widthz <- iris$Petal.Lengthgrps <- as.factor(iris$Species)# Plotlibrary(scatterplot3d)scatterplot3d(x, y, z, pch = 16)

Scatter Plots - R Base Graphs - Easy Guides - Wiki (7)

# Change color by groups# add grids and remove the box around the plot# Change axis labels: xlab, ylab and zlabcolors <- c("#999999", "#E69F00", "#56B4E9")scatterplot3d(x, y, z, pch = 16, color = colors[grps], grid = TRUE, box = FALSE, xlab = "Sepal length", ylab = "Sepal width", zlab = "Petal length")

Scatter Plots - R Base Graphs - Easy Guides - Wiki (8)

  • Read more about static and interactive 3D scatter plot:
    • R base scatterplot3D
    • Amazing interactive 3D scatter plots
    • Impressive package for 3D and 4D graph
    • A complete guide to interactive 3D visualization device system in R

Create a scatter plot:

  • Using R base function:
with(mtcars, plot(wt, mpg, frame = FALSE))
  • Using car package:
car::scatterplot(wt ~ mpg, data = mtcars, smoother = FALSE, grid = FALSE)
  • 3D scatter plot:
library(scatterplot3d)with(iris, scatterplot3d(x = Sepal.Length, y = Sepal.Width, z = Petal.Length, pch = 16, grid = TRUE, box = FALSE))
  • Creating and Saving Graphs in R
  • Scatter Plot Matrices
  • Box Plots
  • Strip Charts: 1-D scatter Plots
  • Bar Plots
  • Line Plots
  • Pie Charts
  • Histogram and Density Plots
  • Dot Charts
  • Plot Group Means and Confidence Intervals
  • Graphical Parameters
  • Lattice Graphs
  • ggplot2 Graphs

This analysis has been performed using R statistical software (ver. 3.2.4).

Scatter Plots - R Base Graphs - Easy Guides - Wiki (2024)
Top Articles
Uncovering The Secrets Of Skirby: Recent Leaks Unveiled
Commack, NY Real Estate & Homes for Sale | realtor.com®
Target Dummies 101 - The Dummy Research/Tutorial Thread
Get maximum control with JCB LiveLink | JCB.com
Guardians Of The Galaxy Showtimes Near Athol Cinemas 8
Cherry Downloadcenter
Cmx Cinemas Gift Card Balance
Santa Maria Cars Craigslist
Live2.Dentrixascend.com
Sirius Mlb Baseball
Guide:Guide to WvW Rewards
Pennymac Mortgage Investment Trust (PMT) Precio de acciones, noticias, cotización e historial de yahoo - Yahoo Finance
Bank Hours Saturday Chase
Sophia Turner Derek Deso Instagram
A Flame Extinguished Wow Bugged
Best 2 Player Tycoons To Play With Friends in Roblox
Overton Funeral Home Waterloo Iowa
Managing Your Activision Account
The Blind Showtimes Near Showcase Cinemas Springdale
Cal Poly San Luis Obispo Catalog
Craigslist Parsippany Nj Rooms For Rent
Server - GIGABYTE Costa Rica
Weather Arlington Radar
Advance Auto Parts Near Me Open Now
Umn Biology
Course schedule | Fall 2022 | Office of the Registrar
Mychart Login Wake Forest
7 Little Words 4/6/23
Shaw Funeral Home Vici Oklahoma
Ltlv Las Vegas
Gwcc Salvage
Hondros Student Portal
How Much Does Hasa Pay For Rent 2022
neither of the twins was arrested,传说中的800句记7000词
Kirby D. Anthoney Now
8 Best Bubble Braid Hairstyles For All Hair Types
Rainfall Map Oklahoma
Gofish Dating
Madden 23 Browns Theme Team
Stark Cjis Court Docket
Hypebeast Muckrack
Craigslist Of Valdosta Georgia
Busted Bell County
Kinda Crazy Craft
Hyundai Elantra - modele, dane, silniki, testy
Honquest Obituaries
Desi Cinemas.com
Noel Berry's Biography: Age, Height, Boyfriend, Family, Net Worth
Jami Lafay Gofundme
Gunsmoke Noonday Devil Cast
Love In Orbit Manga Buddy
Unblocked Games 76 Bitlife
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6395

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.