# Adjust Space Between ggplot2 Axis Labels and Plot Area in R (2 Examples)

In this R programming tutorial, I’ll explain how to adjust the space between the ggplot2 axis labels and the plot area.

The article contains the following content:

All right. So first I’m going to create some example data and a basic plot…

## Creation of Example Data & Basic Plot

Before we can draw our basic ggplot, we need to create some example data frame. Consider the following R code:

```data <- data.frame(x = c("A", "B", "C", "D", "E"), # Create example data y = c(0.3, 0.8, 0.9, 0.2, 0.6))```

In order to plot this example data, we need to install and load the ggplot2 package to R:

```install.packages("ggplot2") # Install ggplot2 package library("ggplot2") # Load ggplot2 package```

Now, we can draw a ggplot barchart with default specifications as follows:

```ggp <- ggplot(data, aes(x, y, fill = y)) + # ggplot2 with default settings geom_bar(stat = "identity") ggp # Print brachart``` Figure 1: Basic ggplot2 Barchart with Default Specifications.

Figure 1 shows our example plot. In our example, we are using a barchart for illustration. However, we could use any other kind of ggplot such as a histogram, a scatterplot, a QQplot, a boxplot, and so on…

Now, let’s adjust the positioning of our plot labels.

## Example 1: Adjust Vertical Space

If we want to adjust the positioning of our label text, we can use the theme and element_text functions as well as the axis.text.x and the vjust commands of the ggplot2 package. Have a look at the following R code:

`ggp + theme(axis.text.x = element_text(vjust = -2)) # Increased vertical space` Figure 2: ggplot2 Barchart with Vertical Adjustment of Labels.

As you can see based on Figure 2, the previous R syntax increased the space between the plot area and the labels of our barchart (as indicated by the red arrows).

Remember: Negative vjust values increase the space vertically; and positive vjust values decrease the space vertically.

Also note that we could move the y-axis labels in the same way by using axis.text.y instead of the axis.text.x command.

## Example 2: Adjust Horizontal Space

If we want to change the horizontal position of our data, we have to use the hjust option instead of the vjust option. Consider the following R code:

`ggp + theme(axis.text.x = element_text(hjust = 5)) # Move labels to the left` Figure 3: ggplot2 Barchart with Horizontal Adjustment of Labels.

As you can see in Figure 3, we just moved our x-axis labels to the left. Play around with the hjust value to adjust the horizontal position as you want.

## Video & Further Resources

I have recently published a video on my YouTube channel, which explains the examples of this tutorial. You can find the video below:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

In addition, you might have a look at the other R tutorials of my website. I have published several tutorials for the ggplot2 package already:

In this R programming tutorial, I explained how to adjust the distance between the x labels and a chart using ggplot2. If you have any further questions, please let me know in the comments below.

Subscribe to the Statistics Globe Newsletter