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

In case you want to learn more about the ggplot2 package and data visualization in R, please have a look at the following video tutorial, where I give a detailed introduction with different examples:

 

 

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 to show relative proportion values in R.

Please note that a similar R code could also be applied to other types of ggplot2 graphics such as line plots or histograms.

If you have additional questions, don’t hesitate to let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

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


4 Comments. Leave new

  • Michal Silber
    December 6, 2022 7:41 pm

    Thanks for your great tutorials and videoes which are perfect for R dummies like me!
    I’m using ggplot for a stacked bar chart. I want the Y axis to be in percentage and I also want the bars to start from the X axis. Since this is using two scale_y_continous lines (scale_y_continuous(expand=c(0,0)) and scale_y_continuous (labels = (scales::percent)), R won’t accept it. How can I solve this?
    Thanks,
    Michal

    Reply

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.

Top