Often you may want to adjust the aspect ratio of a plot created in ggplot2 so that the length and the width of the plot have a specific ratio.

The easiest way to do this is by using the **coord_fixed()** function with the following basic syntax:

ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(ratio=10)

Note that the value provided to the **ratio** argument of the **coord_fixed()** function specifies the aspect ratio, expressed in terms of y/x.

In this particular example we use a **ratio** value of 10 to specify that the y-axis should have units that are 10 times longer than those on the x-axis.

Depending on the x-limits and y-limits of your plot, you may need to adjust the value provided to the **ratio** argument to make the plot easier to read.

The following examples shows how to fix the aspect ratio of a plot created in ggplot2 in practice.

**Example: How to Fix the Aspect Ratio in ggplot2**

First, we will create the following data frame in R that contains 100 observations with measurements on one variable named **x** and another variable named **y**:

#make this example reproducible set.seed(1) #create data frame df <- data.frame(x=runif(100, 0, 50), y=runif(100, 0, 10)) #view head of data frame head(df) x y 1 13.27543 6.547239 2 18.60619 3.531973 3 28.64267 2.702601 4 45.41039 9.926841 5 10.08410 6.334933 6 44.91948 2.132081

Note that we used the **runif()** function in R to generate random values from a uniform distribution, using the following basic syntax:

**runif(n, min, max)**

where:

**n**: Number of values to generate from uniform distribution**min**: Minimum value of uniform distribution**max**: Maximum value of uniform distribution

For the **x** variable, we specified that it should have a range between 0 and 50.

For the **y** variable, we specified that it should have a range between 0 and 10.

Suppose we use the following syntax to create a scatterplot to visualize the 100 observations in this dataset:

library(ggplot2) #create scatterplot to visualize x vs. y ggplot(df, aes(x=x, y=y)) + geom_point()

This produces the following plot:

Notice that the length of the x-axis and y-axis are both the same, despite representing different axis limits.

We can use the **coord_fixed()** argument with a ratio value of 1 to instead use the same units for both the x-axis and y-axis of the plot:

library(ggplot2) #create scatterplot to visualize x vs. y with fixed aspect ratio ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(ratio=1)

This produces the following plot:

Since the **x** variable has a range that is five times larger than the range for the **y** variable, the x-axis is five times larger than the y-axis.

This allows us to visualize the values in the scatterplot using the actual units from the dataset without distorting the data in any way.

Feel free to use a different value for the ratio argument of the **coord_fixed()** argument if you would like to use a different aspect ratio.

**Note**: You can find the complete documentation for the **coord_fixed()** function here.

**Additional Resources**

The following tutorials explain how to perform other common operations in ggplot2:

How to Change Font Size in ggplot2

How to Rotate Axis Labels in ggplot2

How to Remove a Legend in ggplot2

How to Remove Axis Labels in ggplot2