Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 10:20
Show Gist options
  • Save aravindhebbali/096bc745c18a9ba47b99260978189920 to your computer and use it in GitHub Desktop.
Save aravindhebbali/096bc745c18a9ba47b99260978189920 to your computer and use it in GitHub Desktop.
ggplot2: Guides - Axes
# install
install.packages('ggplot2')
install.packages('readr')
# load
library(ggplot2)
library(readr)
# X Axis - Continuous
ggplot(mtcars) +
geom_point(aes(disp, mpg))
# Axis Label
ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(name = "Displacement")
# Axis Limits
ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(limits = c(0, 600))
# Axis Breaks
ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(breaks = c(0, 150, 300, 450, 600))
# Axis Tick Labels
ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(breaks = c(0, 150, 300, 450, 600),
labels = c('0', '150', '300', '450', '600'))
# Axis Position
ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(position = 'top')
# X Axis
ggplot(mtcars) + geom_point(aes(disp, mpg)) +
scale_x_continuous(name = "Displacement", limits = c(0, 600),
breaks = c(0, 150, 300, 450, 600), position = 'top',
labels = c('0', '150', '300', '450', '600'))
# Y Axis - Continuous
ggplot(mtcars) + geom_point(aes(disp, mpg)) +
scale_y_continuous(name = "Miles Per Gallon", limits = c(0, 45),
breaks = c(0, 15, 30, 45), position = 'right',
labels = c('0', '15', '30', '45'))
# Axis Label
ggplot(mtcars) +
geom_bar(aes(factor(cyl))) +
scale_x_discrete(name = "Number of Cylinders")
# Axis Tick Labels
ggplot(mtcars) +
geom_bar(aes(factor(cyl))) +
scale_x_discrete(labels = c("4" = "Four", "6" = "Six", "8" = "Eight"))
# Axis Breaks
ggplot(mtcars) +
geom_bar(aes(factor(cyl))) +
scale_x_discrete(breaks = c("4", "6", "8"))
# Axis Position
ggplot(mtcars) +
geom_bar(aes(factor(cyl))) +
scale_x_discrete(position = 'bottom')
# X Axis - Discrete
ggplot(mtcars) + geom_bar(aes(factor(cyl))) +
scale_x_discrete(name = "Number of Cylinders",
labels = c("4" = "Four", "6" = "Six", "8" = "Eight"),
breaks = c("4", "6", "8"), position = "bottom")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment