In this module, we will continue to explore different ways to modify/customize the legend of plots including:
library(ggplot2)
library(dplyr)
library(tidyr)
ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
guide = guide_legend(label.position = "right"))
ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
guide = guide_legend(label.hjust = 0.5))
ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
guide = guide_legend(direction = "horizontal"))
ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
guide = guide_legend(nrow = 2))
ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
guide = guide_legend(reverse = TRUE))
ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
guide = guide_legend(title = "Cylinders", title.hjust = 0.5,
title.position = "top", label.position = "right",
direction = "horizontal", label.hjust = 0.5, nrow = 2, reverse = TRUE))
ggplot(mtcars) + geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
title = "Horsepower", title.position = "top", title.vjust = 1))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
label.vjust = 0.8))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
barwidth = 10))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
barheight = 3))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
nbin = 4))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
ticks = FALSE))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp)) +
scale_color_continuous(guide = guide_colorbar(
draw.ulim = TRUE, draw.llim = FALSE))
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp,
size = qsec, shape = factor(gear))) +
guides(color = "colorbar", shape = "legend", size = "legend")
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = hp, size = wt, shape = factor(gear))) +
guides(color = guide_colorbar(title = "Horsepower"),
shape = guide_legend(title = "Weight"),
size = guide_legend(title = "Gear"))