How to use coord_cartesian() In R Language

This method is most helpful as it displays the plot within the limits of the axis without clipping the actual data. It just zooms in and shows the area under limits. The coord_cartesian() function will be used to reject all outliers that exceed or below a given quartile

Syntax: plot + coord_cartesian(xlim = <lim-vec>, ylim = <lim-vec>)

Example: Set Axis Limits in ggplot2 in R

R




# Create sample data
set.seed(5642)   
sample_data <- data.frame(x = rnorm(1000),        
                    y = rnorm(1000))
  
# Load ggplot2
library("ggplot2"
  
# create plot with limits set
ggplot(sample_data, aes(x = x, y = y)) +
geom_point() +
coord_cartesian(xlim = c(-5, 1), ylim = c(0, 5))


Output:

How to set axis limits in ggplot2 in R?

In this article, we are going to see how to set the axis limit in ggplot2 in R Programming Language.

Similar Reads

Method 1: Using coord_cartesian()

This method is most helpful as it displays the plot within the limits of the axis without clipping the actual data. It just zooms in and shows the area under limits. The coord_cartesian() function will be used to reject all outliers that exceed or below a given quartile...

Method 2: Using xlim() and ylim()

...