You will learn how to plot smooth line using ggplot2.
Contents:
Related Book
GGPlot2 Essentials for Great Data Visualization in RPrerequisites
- Load the ggplot2 package and set the default theme to
theme_minimal()
:
library(ggplot2)
theme_set(
theme_bw() +
theme(legend.position = "top")
)
- Demo dataset:
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
Key R function: geom_smooth()
- Key R function:
geom_smooth()
for adding smoothed conditional means / regression line. - Key arguments:
color
,size
andlinetype
: Change the line color, size and type.fill
: Change the fill color of the confidence region.
A simplified format of the function `geom_smooth():
geom_smooth(method="auto", se=TRUE, fullrange=FALSE, level=0.95)
- 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 formula = y ~ poly(x, 3) to specify a degree 3 polynomial.
- se : logical value. If TRUE, confidence interval is displayed around smooth.
- fullrange : logical value. If TRUE, the fit spans the full range of the plot
- level : level of confidence interval to use. Default value is 0.95
Regression line
To add a regression line on a scatter plot, the function geom_smooth()
is used in combination with the argument method = lm
. lm
stands for linear model.
p <- ggplot(cars, aes(speed, dist)) +
geom_point()
# Add regression line
p + geom_smooth(method = lm)
# loess method: local regression fitting
p + geom_smooth(method = "loess")
Loess method for local regression fitting
# loess method: local regression fitting
p + geom_smooth(method = "loess")
Polynomial interpolation
# Remove the confidence bande: se = FALSE
p + geom_smooth(method = "lm", formula = y ~ poly(x, 3), se = FALSE)
Spline interpolation
spline.d <- as.data.frame(spline(cars$speed, cars$dist))
p + geom_line(data = spline.d, aes(x = x, y = y))
Recommended for you
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 Stanford
- 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
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
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
Amazon FBA
Amazing Selling Machine
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
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
Version: Français
No Comments