4.2 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Use xlim() and ylim() in R

How to Use xlim() and ylim() in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the xlim() and ylim() functions to set the x-axis limits and y-axis limits of plots in R.

The following examples show how to use these functions in practice.

Example 1: Use xlim() to Set X-Axis Limits

The following code shows how to create a scatterplot in R and specify the x-axis limits using the xlim() function:

#define data frame
df frame(x=c(1, 3, 4, 5, 7, 9),
                 y=c(7, 7, 8, 12, 15, 19))

#create scatterplot with x-axis limits ranging from 0 to 20
plot(df$x, df$y, pch=19, xlim=c(0, 20))

xlim function in R

Example 2: Use ylim() to Set Y-Axis Limits

The following code shows how to create a scatterplot in R and specify the y-axis limits using the ylim() function:

#define data frame
df frame(x=c(1, 3, 4, 5, 7, 9),
                 y=c(7, 7, 8, 12, 15, 19))

#create scatterplot with y-axis limits ranging from 0 to 30
plot(df$x, df$y, pch=19, ylim=c(0, 30))

Example 3: Use xlim() & ylim() to Set Axis Limits

The following code shows how to create a scatterplot in R and specify both the x-axis limits and the y-axis limits:

#define data frame
df frame(x=c(1, 3, 4, 5, 7, 9),
                 y=c(7, 7, 8, 12, 15, 19))

#create scatterplot and specify both x-axis limits and y-axis limits
plot(df$x, df$y, pch=19, xlim=c(0, 20), ylim=c(0, 30))

Additional Resources

How to Set Axis Limits in ggplot2
How to Set Axis Breaks in ggplot2
How to Create a Log Scale in ggplot2
How to Create a Log-Log Plot in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories