2025-03-31
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point()
Map data to aesthetic
mpg |>
ggplot(aes(x = displ, y = hwy,
size = cty)) +
geom_point()
Apply to all points
mpg |>
ggplot(aes(x = displ,
y = hwy)) +
geom_point(size = 0.5)
mpg |>
ggplot(aes(x = displ, y = hwy,
alpha = cty)) +
geom_point()
mpg |>
ggplot(aes(x = displ,
y = hwy)) +
geom_point(alpha = 0.25)
mpg |>
ggplot(aes(x = displ, y = hwy,
shape = class)) +
geom_point()
mpg |>
ggplot(aes(x = displ,
y = hwy)) +
geom_point(shape = 5)
mpg |>
ggplot(aes(x = displ, y = hwy,
color = class)) +
geom_point()
mpg |>
ggplot(aes(x = displ,
y = hwy)) +
geom_point(color = "dodgerblue")
What happens if we put a single color in the aesthetic?
mpg |>
ggplot(aes(x = displ, y = hwy, color = "dodgerblue")) +
geom_point()
mpg |>
ggplot(aes(x = displ, y = hwy, group = drv)) +
geom_smooth()
mpg |>
ggplot(aes(x = displ, y = hwy, linetype = drv)) +
geom_smooth()
mpg |>
ggplot(aes(x = displ, y = hwy, color = drv)) +
geom_smooth(show.legend = FALSE)
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point(aes(color = class)) +
geom_smooth()
mpg |>
ggplot(aes(x = displ, y = hwy, color = drv)) +
geom_point() +
geom_smooth(aes(linetype = drv))
ggplot()
can calculate statistics on the flyCount observations of variable types with stat_count()
stat_summary()
mpg |>
ggplot(aes(x = class, y = hwy)) +
geom_point()
mpg |>
ggplot(aes(x = class, y = hwy)) +
stat_summary()
Plot mean and 95% CI
mpg |>
ggplot(aes(x = class, y = hwy)) +
stat_summary(fun.data = mean_cl_normal)
Plot median and range
mpg |>
ggplot(aes(x = class, y = hwy)) +
stat_summary(fun.min = min, fun.max = max, fun = median)
Coloring by group difficult to visualize with many groups
mpg |>
ggplot(aes(x = displ, y = hwy, color = class)) +
geom_point()
Pulls out groups into separate panels
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_wrap(vars(class))
Group by two or more variables with facet_grid()
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(rows = vars(year), cols = vars(drv))
Allow axes to vary with scales
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(rows = vars(year), cols = vars(drv),
scales = "free_x")
Allow axes to vary with scales
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point() +
facet_grid(rows = vars(year), cols = vars(drv),
scales = "free")