Modify legend when the aesthetics are mapped to variables. Specifically
Quite often the aesthetics are mapped to variables ggplot2 automatically creates legends wherever applicable. You may want to modify the appearance of legends. In this module, we will learn to modify the legends when the following aesthetics are mapped to categorical/continuous variables
library(ggplot2)
library(dplyr)
library(tidyr)
If you have mapped aesthetics to a continuous variable, you can modify the legend
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp))
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp)) +
scale_size_continuous(name = "Horsepower")
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp)) +
scale_size_continuous(range = c(3, 6))
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp)) +
scale_size_continuous(limits = c(0, 400))
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp)) +
scale_size_continuous(breaks = c(100, 200, 300, 400))
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp)) +
scale_size_continuous(breaks = c(100, 200, 300, 400),
labels = c("Hundred", "2 Hundred", "3 Hundred", "4 Hundred"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, size = hp)) +
scale_size_continuous(name = "Horsepower", range = c(3, 6),
limits = c(0, 400), breaks = c(100, 200, 300, 400),
labels = c("Hundred", "2 Hundred", "3 Hundred", "4 Hundred"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue')
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
scale_alpha_continuous("Horsepower")
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
scale_alpha_continuous(breaks = c(0, 100, 200, 300, 400))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
scale_alpha_continuous(limits = c(0, 400))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
scale_alpha_continuous(range = c(0.4, 0.8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
scale_alpha_continuous(breaks = c(0, 100, 200, 300, 400),
labels = c("Zero", "Hundred", "2 Hundred",
"3 Hundred", "4 Hundred"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
scale_alpha_continuous("Horsepower", breaks = c(0, 100, 200, 300, 400),
limits = c(0, 400), range = c(0.4, 0.8),
labels = c("Zero", "Hundred", "2 Hundred",
"3 Hundred", "4 Hundred"))
If you have mapped aesthetics to a discrete variable, you can to modify legend:
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl)))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(name = "Cylinders",
values = c("red", "blue", "green"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
limits = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
labels = c('Four', 'Six', 'Eight'))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(name = "Cylinders", values = c("red", "blue", "green"),
labels = c('Four', 'Six', 'Eight'), limits = c(4, 6, 8), breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22)
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(name = "Cylinders",
values = c("red", "blue", "green"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(values = c("red", "blue", "green"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(values = c("red", "blue", "green"),
labels = c('Four', 'Six', 'Eight'))
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(values = c("red", "blue", "green"),
limits = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(values = c("red", "blue", "green"),
breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(name = "Cylinders", values = c("red", "blue", "green"),
labels = c('Four', 'Six', 'Eight'), limits = c(4, 6, 8), breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl)))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape_manual(name = "Cylinders",values = c(4, 12, 24))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape_manual(values = c(4, 12, 24))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape_manual(values = c(4, 12, 24), labels = c('Four', 'Six', 'Eight'))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape_manual(values = c(4, 12, 24), limits = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape_manual(values = c(4, 12, 24), breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape_manual(name = "Cylinders", labels = c('Four', 'Six', 'Eight'),
values = c(4, 12, 24), limits = c(4, 6, 8), breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue')
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
scale_alpha_discrete(name = "Cylinders")
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
scale_alpha_discrete(breaks = c("4", "6", "8"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
scale_alpha_discrete(range = c(0.4, 0.8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
scale_alpha_discrete(labels = c("Four", "Six", "Eight"))
ggplot(mtcars) +
geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
scale_alpha_discrete(name = "Cylinders", range = c(0.4, 0.8),
breaks = c("4", "6", "8"), labels = c("Four", "Six", "Eight"))
If you have mapped shape/size to a discrete variable which has less than six categories, you can use the following functions:
scale_shape()
scale_size()
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape()
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape(name = 'Cylinders')
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape(labels = c('Four', 'Six', 'Eight'))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape(limits = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape(breaks = c(4, 6, 8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape(solid = FALSE)
ggplot(mtcars) +
geom_point(aes(disp, mpg, shape = factor(cyl))) +
scale_shape(solid = FALSE, name = 'Cylinders', limits = c(4, 6, 8),
labels = c('Four', 'Six', 'Eight'), breaks = c(4, 6, 8))