简体   繁体   中英

R ggplot scale histogram according to bin size

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)")

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)")

在此处输入图像描述

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") 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM