Change Y-Axis to Percentage Points in ggplot2 Barplot in R (2 Examples)

 

In this R tutorial you’ll learn how to set the axis labels of a barchart in percentage points.

The tutorial contains this:

Let’s dive right into the examples…

 

Example Data, Packages & Basic Graphic

We’ll use the data below as basement for this R programming tutorial:

data <- data.frame(x = LETTERS[1:5],    # Example data
                   y = c(0.4, 0.7, 0.1, 0.3, 0.6))
data                                    # Print data
#   x   y
# 1 A 0.4
# 2 B 0.7
# 3 C 0.1
# 4 D 0.3
# 5 E 0.6

The previous output of the RStudio console shows that our example data has five rows and two columns.

If we want to use the functions of the ggplot2 package, we also have to install and load ggplot2:

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

Next, we can draw the example data.

ggp <- ggplot(data, aes(x, y)) +        # ggplot2 with default y-axis labels
  geom_bar(stat = "identity")
ggp                                     # Draw plot

 

r graph figure 1 change y axis-percentages ggplot2 barplot r

 

The output of the previous code is shown in Figure 1 – A ggplot2 barchart with default axis values.

 

Example 1: Set Y-Axis to Percent Using scale_y_continuous Function

In Example 1, I’ll show how to customize axis values of a barchart using the scale_y_continuous function. Have a look at the following R syntax and the resulting graphic:

ggp +                                   # Change y-axis to percent
  scale_y_continuous(labels = scales::percent)

 

r graph figure 2 change y axis-percentages ggplot2 barplot r

 

Figure 2 shows the output of the previously shown R syntax: A ggplot2 barchart with percentage points as y-axis labels.

You can also see that the percentage points are shown with one digit after the decimal point. In the next example, I’ll show how to change that…

 

Example 2: Set Y-Axis to Percent with User-Defined Accuracy

The following R programming syntax explains how to adjust the accuracy (i.e. how precise the percentage points are rounded) of the percentages on the y-axis:

ggp +                                   # Specify accuracy of y-axis
  scale_y_continuous(labels = scales::percent_format(accuracy = 1))

 

r graph figure 3 change y axis-percentages ggplot2 barplot r

 

As illustrated in Figure 3, we plotted a barchart with percentage points rounded to zero digits.

 

Video & Further Resources

I have recently released a video on my YouTube channel, which illustrates the contents of this article. You can find the video below.

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the related tutorials on this homepage:

 

At this point you should know how to adjust ggplot2 axis labels of a barplot in R. If you have additional questions, don’t hesitate to let me know in the comments section.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top