A scatter plot displays the relationship between two continuous variables. In ggplot2, we can build a scatter plot using geom_point()
. Scatterplots can show you visually
library(ggplot2)
library(dplyr)
library(tidyr)
ggplot(mtcars) +
geom_point(aes(disp, mpg))
ggplot(mtcars) +
geom_point(aes(disp, mpg), position = 'jitter')
ggplot(mtcars) +
geom_jitter(aes(disp, mpg))
Now let us modify the aesthetics of the points. There are two ways:
aes()
functionggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl)),
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp),
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg), color = 'blue',
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg), color = 'blue', alpha = 0.4,
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl)),
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg), shape = 3,
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp), color = 'blue',
position = 'jitter')
ggplot(mtcars) +
geom_point(aes(disp, mpg), size = 3,
position = 'jitter')
ggplot(mtcars, aes(disp, mpg)) +
geom_point(position = 'jitter') +
geom_smooth(method = 'lm', se = FALSE)
ggplot(mtcars, aes(disp, mpg)) +
geom_point(position = 'jitter') +
geom_smooth(method = 'lm', se = TRUE)
ggplot(mtcars, aes(disp, mpg)) +
geom_point(position = 'jitter') +
geom_smooth(method = 'loess', se = FALSE)
ggplot(mtcars, aes(disp, mpg)) +
geom_point(position = 'jitter') +
geom_abline(intercept = 29.59985, slope = -0.04122)