Convenience functions to set the axis limits.




If numeric, will create a continuous scale, if factor or character, will create a discrete scale. For lims, every argument must be named.


Observations not in this range will be dropped completely and not passed to any other layers. If a NA value is substituted for one of the limits that limit is automatically calculated.


# xlim xlim(15, 20)
<ScaleContinuousPosition> Range: Limits: 15 -- 20
xlim(20, 15)
<ScaleContinuousPosition> Range: Limits: -20 -- -15
xlim(c(10, 20))
<ScaleContinuousPosition> Range: Limits: 10 -- 20
xlim("a", "b", "c")
<ggproto object: Class ScaleDiscretePosition, ScaleDiscrete, Scale> aesthetics: x xmin xmax xend break_info: function break_positions: function breaks: waiver call: call clone: function dimension: function drop: TRUE expand: waiver get_breaks: function get_breaks_minor: function get_labels: function get_limits: function guide: none is_discrete: function is_empty: function labels: waiver limits: a b c map: function map_df: function na.value: NA name: waiver palette: function range: <ggproto object: Class RangeDiscrete, Range> range: NULL reset: function train: function super: <ggproto object: Class RangeDiscrete, Range> range_c: <ggproto object: Class RangeContinuous, Range> range: NULL reset: function train: function super: <ggproto object: Class RangeContinuous, Range> reset: function scale_name: position_d train: function train_df: function transform: function transform_df: function super: <ggproto object: Class ScaleDiscretePosition, ScaleDiscrete, Scale>
ggplot(mtcars, aes(mpg, wt)) + geom_point() + xlim(15, 20)
Warning message: Removed 19 rows containing missing values (geom_point).

# with automatic lower limit ggplot(mtcars, aes(mpg, wt)) + geom_point() + xlim(NA, 20)
Warning message: Removed 14 rows containing missing values (geom_point).

# Change both xlim and ylim ggplot(mtcars, aes(mpg, wt)) + geom_point() + lims(x = c(10, 20), y = c(3, 5))
Warning message: Removed 18 rows containing missing values (geom_point).

See also

For changing x or y axis limits without dropping data observations, see coord_cartesian.