Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 10:29
Show Gist options
  • Save aravindhebbali/f099b954fa8f5a84cd8e5a2a031f91db to your computer and use it in GitHub Desktop.
Save aravindhebbali/f099b954fa8f5a84cd8e5a2a031f91db to your computer and use it in GitHub Desktop.
ggplot2: Legends - Part 1
# install
install.packages('ggplot2')
install.packages('readr')
# library
library(ggplot2)
library(readr)
# Basic Plot
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl)))
# Title
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(name = "Cylinders",
values = c("red", "blue", "green"))
# Values
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"))
# Labels
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
limits = c(4, 6, 8))
# Limits
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
labels = c('Four', 'Six', 'Eight'))
# Breaks
ggplot(mtcars) +
geom_point(aes(disp, mpg, color = factor(cyl))) +
scale_color_manual(values = c("red", "blue", "green"),
breaks = c(4, 6, 8))
# Putting it all together
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment