Table of Contents
The process of displaying percentages on a histogram in ggplot2 involves using the “stat_bin” function to calculate the percentage of each category and then using the “geom_histogram” function to create the histogram. This can be achieved by setting the “aes” attribute to the “y” axis and using the “scale_y_continuous” function to format the labels as percentages. This allows for a more precise representation of the data and enhances the visual display of the histogram.
Display Percentages on Histogram in ggplot2
You can use the following basic syntax to display percentages on the y-axis of a histogram in ggplot2:
library(ggplot2) library(scales) #create histogram with percentages ggplot(data, aes(x = factor(team))) + geom_bar(aes(y = (..count..)/sum(..count..))) + scale_y_continuous(labels=percent)
The following examples show how to use this syntax in practice.
Example 1: Basic Histogram with Percentages
The following code shows how to create a histogram for categorical variables with percentages displayed on the y-axis:
library(ggplot2) library(scales) #define data frame data <- data.frame(team = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C'), points = c(77, 79, 93, 85, 89, 99, 90, 80, 68, 91, 92)) #create histogram with percentages ggplot(data, aes(x = factor(team))) + geom_bar(aes(y = (..count..)/sum(..count..))) + scale_y_continuous(labels=percent)

Example 2: Histogram with Percentages (Remove Decimal Places)
You can use the accuracy argument to only display whole numbers as percentages on the y-axis as well:
library(ggplot2) library(scales) #define data frame data <- data.frame(team = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C'), points = c(77, 79, 93, 85, 89, 99, 90, 80, 68, 91, 92)) #create histogram with percentages ggplot(data, aes(x = factor(team))) + geom_bar(aes(y = (..count..)/sum(..count..))) + scale_y_continuous(labels = scales::percent_format(accuracy = 1L))

Example 3: Custom Histogram with Percentages
The following code shows how to create a histogram with percentages shown on the y-axis and a custom title, axis labels, and colors:
library(ggplot2) library(scales) #define data frame data <- data.frame(team = c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C'), points = c(77, 79, 93, 85, 89, 99, 90, 80, 68, 91, 92)) #create histogram with percentages and custom aesthetics ggplot(data, aes(x = factor(team))) + geom_bar(aes(y = (..count..)/sum(..count..)), fill = 'lightblue') + scale_y_continuous(labels=percent) + labs(title = 'Breakdown by Team', x = 'Team', y = 'Percent of Total') + theme_minimal()

Cite this article
stats writer (2024). How can I display percentages on a histogram in ggplot2?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-display-percentages-on-a-histogram-in-ggplot2/
stats writer. "How can I display percentages on a histogram in ggplot2?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-display-percentages-on-a-histogram-in-ggplot2/.
stats writer. "How can I display percentages on a histogram in ggplot2?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-display-percentages-on-a-histogram-in-ggplot2/.
stats writer (2024) 'How can I display percentages on a histogram in ggplot2?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-display-percentages-on-a-histogram-in-ggplot2/.
[1] stats writer, "How can I display percentages on a histogram in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I display percentages on a histogram in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
