# Zoom into a specified area
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
xlim(15, 20)
# reverse scale
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
xlim(20, 15)
# with automatic lower limit
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
xlim(NA, 20)
# You can also supply limits that are larger than the data.
# This is useful if you want to match scales across different plots
small <- subset(mtcars, cyl == 4)
big <- subset(mtcars, cyl > 4)
ggplot(small, aes(mpg, wt, colour = factor(cyl))) +
geom_point() +
lims(colour = c("4", "6", "8"))
ggplot(big, aes(mpg, wt, colour = factor(cyl))) +
geom_point() +
lims(colour = c("4", "6", "8"))
Run the code above in your browser using DataLab