Home > front end >  R ggplot scale histogram according to bin size
R ggplot scale histogram according to bin size

Time:02-18

I'm trying to use ggplot to make a histogram where the area of each bin is the total number of occurrences in each bin. So in essence I'm looking to scale the histogram height down by a factor of the bin height. I can create a normal histogram without issue, but I'm not quite sure how to scale the histogram in such a manner. The data and unscaled-histogram are given below. Any help would be much appreciated.

times <- c(71, 72, 73, 74, 75, 76, 77, 78, 79, 80)
occurrences <- c(2, 0, 3, 5, 4, 1, 3, 1, 0, 1)
df <- data.frame("times" = rep(times, occurrences))

ggplot(df, aes(times))   geom_histogram(breaks=seq(70.5,80.5,2), 
  colour = "black", size = 2)   ylab("Frequencies")   ylim(0,10)   
  ggtitle("Bin Width = 2 (un-scaled)")

CodePudding user response:

You can add y = ..count../2 to scale the histogram according to your bin width:

ggplot(df, aes(times))   
  geom_histogram(aes(y = ..count../2), breaks=seq(70.5,80.5,2),  colour = "black", size = 2)   
  ylab("Frequencies")   ylim(0,10)   
  ggtitle("Bin Width = 2 (scaled)")

enter image description here

CodePudding user response:

I think what you need is a barplot using geom_col()

library(dplyr)
library(ggplot2)

df %>% 
  count(times) %>% 
  ggplot(aes(x=factor(times), y=n))  
  geom_col(colour = "black", size = 2)   
  ylab("Frequencies")   
  ylim(0,6)  
  xlab("times") 

enter image description here

  • Related