Changing the Range of Axes with xlim and ylim with R

Changing the Range of Axes with xlim and ylim

Understanding xlim and ylim

  • xlim: Specifies the range of the x-axis.
  • ylim: Specifies the range of the y-axis.

Both options accept a numeric vector with two values:

  • c(min, max): The minimum and maximum values of the axis.

Using xlim and ylim in the plot() Function

You can specify the ranges of the x-axis and y-axis directly when creating a plot using the plot() function.

Example: 

# Generate example data
x <- 1:10
y <- x^2
# Create a plot with default axis ranges
plot(x, y, main = "Default Axis Ranges")
# Create a plot with customized x-axis and y-axis ranges
plot(x, y, main = "Customized Axis Ranges", xlim = c(0, 12), ylim = c(0, 120))
  • xlim = c(0, 12): Sets the x-axis to range from 0 to 12.
  • ylim = c(0, 120): Sets the y-axis to range from 0 to 120.

Adjusting Axis Ranges After Plot Creation

You can also adjust the axis ranges of an existing plot by using functions like axis() and plot.window().

Example: 

# Create a basic plot with default ranges
plot(x, y, main = "Plot with Adjustable Ranges")
# Adjust the x-axis and y-axis ranges after plot creation
plot.window(xlim = c(0, 12), ylim = c(0, 120))
axis(1, at = seq(0, 12, by = 2))  # Custom x-axis
axis(2, at = seq(0, 120, by = 20))  # Custom y-axis
  • plot.window(): Adjusts the plotting region.
  • axis(): Adds custom axis ticks and labels.

Using xlim and ylim for Zooming

By setting xlim and ylim, you can zoom in on a specific region of your data.

Example: 

# Generate example data with a large range
x <- 1:100
y <- x^2
# Zoom in on a specific region of the data
plot(x, y, main = "Zoomed-in View", xlim = c(30, 70), ylim = c(900, 4900))
  • xlim = c(30, 70): Zooms in on the x-axis range from 30 to 70.
  • ylim = c(900, 4900): Zooms in on the y-axis range from 900 to 4900.

Handling Missing Values and Outliers

When adjusting axis ranges, you may want to handle missing values and outliers to ensure that the plot remains informative.

Example: 

# Generate data with outliers
x <- 1:10
y <- c(1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 1000)
# Create a plot with customized ranges to handle outliers
plot(x, y, main = "Handling Outliers", xlim = c(0, 12), ylim = c(0, 150))
  • xlim = c(0, 12): Ensures the x-axis accommodates the full range of data.
  • ylim = c(0, 150): Adjusts the y-axis to handle extreme values.

Combining xlim and ylim with Other Plotting Parameters

You can combine xlim and ylim with other plotting parameters for a more customized plot.

Example: 

# Generate example data
x <- 1:10
y <- x^2
# Create a plot with customized axis ranges, colors, and point types
plot(x, y, main = "Customized Plot", xlim = c(0, 12), ylim = c(0, 120),
     col = "blue", pch = 16, lty = 2)
# Add lines and text
abline(h = 50, col = "red", lty = 3)
text(6, 60, "Annotation", cex = 1.5)
  • col = “blue”: Sets the color of the points.
  • pch = 16: Uses filled circles for points.
  • lty = 2: Adds dashed lines to the plot.
  • abline(): Adds a horizontal line at y = 50.
  • text(): Adds annotation text.

Summary

  • xlim and ylim Parameters: Control the ranges of the x and y axes.
    xlim = c(min, max): Range of the x axis.
    ylim = c(min, max): Range of the y axis.
  • Customizing Axis Ranges: Specify ranges when creating the chart with plot().
  • Post-Creation Adjustment: Use plot.window() and axis() to adjust ranges and ticks after the chart is created.
  • Zoom to a Specific Region: Adjust ranges to zoom in on specific regions of the data.
  • Missing Values ​​and Outliers: Adjust ranges to handle extreme values ​​and outliers.
  • Combine with Other Parameters: Combine xlim and ylim with other parameters to further customize your charts.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print