Smooth A Scatterplot Trend Line

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# load the ggplot2 library
library(ggplot2)

# set the seed so we can reproduce the results
set.seed(1410)
# create a variable that is the first 100 rows of the diamonds dataset
dsmall <- diamonds[sample(nrow(diamonds), 100), ]
# create a scatterplot with a smoothing line with a wiggly trend line
p1 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 0.2); p1
geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

png

# create a scatterplot with a smoothing line with a smooth trend line
p2 <- qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 1); p2
geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

png