Set Aspect Ratio of Scatter Plot and Bar Plot in R Programming – Using asp in plot() Function

asp is a parameter of the plot() function in R Language is used to set aspect ratio of plots (Scatterplot and Barplot). Aspect ratio is defined as proportional relationship between width and height of the plot axes.

Syntax: plot(x, y, asp )

Parameters:
x, y: Coordinates of x and y axis
asp: Aspect ratio

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Set seed for reproducibility
set.seed(86000)     
  
# Create random x variable
x <- runif(120)     
  
# Create y variable correlated with x
y <- x + runif(120)      
  
# Plot without setting aspect ratio
plot(x, y)       
  
# Plot with asp = 5
plot(x, y, asp = 5)         

chevron_right


Output:



  • Plot Without Aspect Ratio:
    Plot-without-aspect-ratio
  • Plot with Aspect Ratio:
    plot-with-aspect-ratio

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Set seed for reproducibility
set.seed(86000)              
  
# Create random x variable
x <- runif(120)              
  
# Create y variable correlated with x
y <- x + runif(120) 
  
# Regular barplot
barplot(x)                   
  
# Barplot with aspect ratio of 5
barplot(x, asp = 5)         

chevron_right


Output:

  • Bar Plot Without Aspect Ratio:
    Barplot-without-aspect-ratio
  • Bar Plot with Aspect Ratio:
    Barplot-with-aspect-ratio

Here, the asp option increases the width of the y-axis.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.