1.Import the library

library(ggplot2)

Two variables: Continuous X, Continuous Y

We’ll use the mtcars data set. The variable cyl is used as grouping variable.

2. Import data set

data(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars[, c("wt", "mpg", "cyl")])
##                      wt  mpg cyl
## Mazda RX4         2.620 21.0   6
## Mazda RX4 Wag     2.875 21.0   6
## Datsun 710        2.320 22.8   4
## Hornet 4 Drive    3.215 21.4   6
## Hornet Sportabout 3.440 18.7   8
## Valiant           3.460 18.1   6
b <- ggplot(mtcars, aes(x = wt, y = mpg))

Possible layers include:

geom_point() for scatter plot

geom_smooth() for adding smoothed line such as regression line

geom_quantile() for adding quantile lines

geom_rug() for adding a marginal rug

geom_jitter() for avoiding overplotting

geom_text() for adding textual annotations

2.1 geom_point(): Scatter plot

# Basic plot
b + geom_point()

# change the color and the point 
# by the levels of cyl variable
b + geom_point(aes(color = cyl, shape = cyl)) 

# Change color manually
b + geom_point(aes(color = cyl, shape = cyl)) +
  scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))+
  theme_minimal()

2.2 geom_smooth(): Add regression line or smoothed conditional mean

# 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.

# Regression line only
b + geom_smooth(method = lm)
## `geom_smooth()` using formula = 'y ~ x'

b + stat_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

# Point + regression line
# Remove the confidence interval 
b + geom_point() + 
  geom_smooth(method = lm, se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

# loess method: local regression fitting
b + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

# Change color and shape by groups (cyl)
b + geom_point(aes(color=cyl, shape=cyl)) + 
  geom_smooth(aes(color=cyl, shape=cyl), 
              method=lm, se=FALSE, fullrange=TRUE)
## Warning in geom_smooth(aes(color = cyl, shape = cyl), method = lm, se = FALSE, :
## Ignoring unknown aesthetics: shape
## `geom_smooth()` using formula = 'y ~ x'

2.3 geom_quantile(): Add quantile lines from a quantile regression

ggplot(mpg, aes(cty, hwy)) +
  geom_point() + geom_quantile() +
  theme_minimal()
## Smoothing formula not specified. Using: y ~ x

ggplot(mpg, aes(cty, hwy)) +
  geom_point() + stat_quantile(quantiles = c(0.25, 0.5, 0.75)) # An alternative to geom_quantile() is the function stat_quantile()
## Smoothing formula not specified. Using: y ~ x

2.4 geom_rug(): Add marginal rug to scatter plots

## Add marginal rugs using faithful data
ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point() + geom_rug()  

2.5 geom_jitter(): Jitter points to reduce overplotting

p <- ggplot(mpg, aes(displ, hwy))
# Default scatter plot
p + geom_point()

# Use jitter to reduce overplotting
p + geom_jitter(position = position_jitter(width = 0.5, height = 0.5))

2.6 geom_text(): Textual annotations

b + geom_text(aes(label = rownames(mtcars)))

http://www.sthda.com/english/wiki/be-awesome-in-ggplot2-a-practical-guide-to-be-highly-effective-r-software-and-data-visualization