Ggplot2 Scatter Plots - Quick Start Guide - R Software and Data Visualization - Easy Guides - Wiki - STHDA
Ggplot2 Scatter Plots - Quick Start Guide - R Software and Data Visualization - Easy Guides - Wiki - STHDA
Licence:
Search...
Support About
Home / Easy Guides / R software / Data Visualization / ggplot2 - Essentials / Actions menu for module Wiki
ggplot2 scatter plots : Quick start guide - R software and data visualization
Tools
This article describes how create a scatter plot using R software and ggplot2 package. The function
geom_point() is used.
Related Book:
library(ggplot2)
# Basic scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# Change the point size, and shape
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23)
Note that, the size of the points can be controlled by the values of a continuous variable as in the ex-
ample below.
geom_abline() has been already described at this link : ggplot2 add straight lines to a plot.
A simplified format is :
method : smoothing method to be used. Possible values are lm, glm, gam, loess, rlm.
method = “loess”: This is the default value for small number of observations. It computes a
smooth local regression. You can read more about loess using the R code ?loess.
method =“lm”: It fits a linear model. Note that, it’s also possible to indicate the formula as for-
mula = y ~ poly(x, 3) to specify a degree 3 polynomial.
Note that a transparent color is used, by default, for the confidence band. This can be changed by us-
ing the argument alpha : geom_smooth( ll=“blue”, alpha=1)
It is also possible to change manually point and line colors using the functions :
geom_rug(sides ="bl")
sides : a string that controls which sides of the plot the rugs appear on. Allowed value is a string containing
any of “trbl”, for top, right, bottom, and left.
plot + geom_bin2d(...)
plot+stat_bin_2d(geom=NULL, bins=30)
plot + stat_summary_2d(geom = NULL, bins = 30, fun = mean)
# Plot
p <- ggplot(diamonds, aes(carat, price))
p + geom_bin2d()
set.seed(1234)
x <- c(rnorm(500, mean = -1), rnorm(500, mean = 1.5))
y <- c(rnorm(500, mean = 1), rnorm(500, mean = 1.7))
group <- as.factor(rep(c(1,2), each=500))
df <- data.frame(x, y, group)
head(df)
## x y group
## 1 -2.20706575 -0.2053334 1
## 2 -0.72257076 1.3014667 1
## 3 0.08444118 -0.5391452 1
## 4 -3.34569770 1.6353707 1
## 5 -0.57087531 1.7029518 1
## 6 -0.49394411 -0.9058829 1
To put multiple plots on the same page, the package gridExtra can be used. Install the package as follow :
install.packages("gridExtra")
Arrange ggplot2 with adapted height and width for each row and column :
library("gridExtra")
grid.arrange(xdensity, blankPlot, scatterPlot, ydensity,
ncol=2, nrow=2, widths=c(4, 1.4), heights=c(1.4, 4))
Read more on how to arrange multiple ggplots in one page : ggplot2 - Easy way to mix multiple graphs on the
same page
# Continuous colors
p + scale_color_brewer(palette="Paired") + theme_classic()
# Discrete colors
p + scale_color_brewer(palette="Dark2") + theme_minimal()
# Gradient colors
p + scale_color_brewer(palette="Accent") + theme_minimal()
Read more on ggplot2 colors here : ggplot2 colors
Infos
This analysis has been performed using R software (ver. 3.2.4) and ggplot2 (ver. 2.1.0)
Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it
on Twitter, Facebook or Linked In.
Show me some love with the like buttons below... Thank you and please don't forget to share and
comment below!!
Recommended for You!
Machine Learning Essentials: Practical Guide to Cluster Ana- Practical Guide to Principal
Practical Guide in R lysis in R Component Methods in R
More books on R and data sci-
R Graphics Essentials for Great Network Analysis and Visualiza- ence
Data Visualization tion in R
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
Course: Machine Learning: Master the Fundamentals by Standford
Specialization: Data Science by Johns Hopkins University
Specialization: Python for Everybody by University of Michigan
Courses: Build Skills for a Top Job in any Industry by Coursera
Specialization: Master Machine Learning Fundamentals by University of Washington
Specialization: Statistics with R by Duke University
Specialization: Software Development in R by Johns Hopkins University
Specialization: Genomic Data Science by Johns Hopkins University
Trending Courses
The Science of Well-Being by Yale University
Google IT Support Professional by Google
Python for Everybody by University of Michigan
IBM Data Science Professional Certificate by IBM
Business Foundations by University of Pennsylvania
Introduction to Psychology by Yale University
Excel Skills for Business by Macquarie University
Psychological First Aid by Johns Hopkins University
Graphic Design by Cal Arts
Others
R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett
Grolemund
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques
to Build Intelligent Systems by Aurelien Géron
Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund &
Hadley Wickham
An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
Deep Learning with R by François Chollet & J.J. Allaire
Deep Learning with Python by François Chollet
Subscribe
by FeedBurner
On Social Networks:
Get involved :
Click to follow us on Facebook and Google+ :
Comment this article by clicking on "Discussion" button (top-right position of this page)
This page has been seen 938174 times
Sign in
Login
Login
Password
Password
Auto connect
Sign in
Register
Forgotten password
Welcome!
Want to Learn More on R Programming and Data Science?
Follow us by Email
Subscribe
by FeedBurner
factoextra
survminer
ggpubr
ggcorrplot
fastqcr
Our Books
R Graphics Essentials for Great Data Visualization: 200 Practical Examples You Want to Know for Data
Science
NEW!!
Practical Guide to Cluster Analysis in R
3D Plots in R
R-Bloggers
Newsletter Email
Boosted by PHPBoost