Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 10:47
Show Gist options
  • Save aravindhebbali/ef2b665173cf7cfe17093f3dcb052035 to your computer and use it in GitHub Desktop.
Save aravindhebbali/ef2b665173cf7cfe17093f3dcb052035 to your computer and use it in GitHub Desktop.
ggplot2: Legends Part - 2
#install
install.packages('ggplot2')
install.packages('readr')
# library
library(ggplot2)
library(readr)
# Plot
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22)
# Title
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(name = "Cylinders",
values = c("red", "blue", "green"))
# Values
ggplot(mtcars) +
geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
scale_fill_manual(values = c("red", "blue", "green"))
# Labels
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'))
# Limits
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))
# Breaks
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))
# Putting it all together...
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment