Change Axis Scales in Base R

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively. This function takes a vector as an argument which contains the values of lower axis limit and higher axis limit.

Syntax: plot( df$xaxis, df$yaxis, xlim, ylim)

where,

  • df: determines the data frame in use.
  • xaxis and yaxis: determine the axis variables for plotting.
  • xlim: determines the vector that contains x-axis limits.
  • ylim: determines the vector that contains y-axis limits.

Example: Basic example where plot axis limits are set from 0 to 2 for the x-axis and from 18 to 20 for the y-axis.

R




# create sample data frame
sample_data <- data.frame(x=rnorm(100),
                          y=rnorm(100)+20)
  
# create plot with custom axis scales
plot(sample_data$x, sample_data$y, xlim=c(0,2),
     ylim=c(18,20))


Output:

Output

Convert axis scales to log scale:

To convert the axis scale log scale in the base R plot, we use the log argument of the plot() function. The log argument converts the given axis into its log scale alternative. This helps us in visualizing the skew data frames.

Syntax: plot( df$xaxis, df$yaxis, log)

where,

  • df: determines the data frame in use.
  • xaxis and yaxis: determine the axis variables for plotting.
  • log: determines the axis which has to be converted in log scale.

Example: Basic example where the x-axis has been converted to its log scale alternative.

R




# create sample data frame
sample_data <- data.frame(x=rnorm(100),
                          y=rnorm(100)+20)
  
# create plot with log x-axis scale
plot(sample_data$x, sample_data$y, log='x')


Output:

Output

How to Change Axis Scales in R Plots?

In this article, we will learn how to change Axis Scales in the R Programming Language.

Similar Reads

Method 1: Change Axis Scales in Base R

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively. This function takes a vector as an argument which contains the values of lower axis limit and higher axis limit....

Method 2: Change Axis Scales in ggplot2

...