I have the following data
dati <- read.table(text="
class num
1 0.0 63530
2 2.5 27061
3 3.5 29938
4 4.5 33076
5 5.6 45759
6 6.5 72794
7 8.0 153177
8 10.8 362124
9 13.5 551051
10 15.5 198634
")
And I want to produce a histogram with variable size bins, so that the area of each bar reflects the total numerosity (num) of each bin. I tried
bins <- c(0,4,8,11,16)
p <- ggplot(dati) +
geom_histogram(aes(x=class,weight=num),breaks = bins)
however, this produces a histogram where the length of each bar is equal to total numerosity of each bin. Because bin widths are variable, areas are not proportional to numerosity. I could not solve this apparently easy problem within ggplot2. Can anyone help me?
I think you are looking for a density plot - this closely related question has most of the answer. You call y = ..density..
in geom_histogram()
.
This works because stat_bin
(recall geom_histogram()
is geom_bar()
+ stat_bin()
, and stat_bin()
constructs a data frame with columns count
and density
. Thus calling y = ..density..
pulls the right column for density, whereas the default (counts) is as if you call y = ..count..
.
##OP's code
ggplot(dati) + geom_histogram(aes(x=class, weight=num),
breaks = bins)
##new code (density plot)
ggplot(dati) + geom_histogram( aes(x=class,y = ..density.., weight=num),
breaks = bins, position = "identity")
You can find some further examples in the online ggplot2 help page for geom_histogram()
.
It sounds to me like you asking for how to produce variable sized bar widths. If so, you just need to call the 'width' parameter in your ggplot aesthetics like this:
ggplot(data, aes(x = x, y = y, width = num))
this method is discussed more in the following question: Variable width bars in ggplot2 barplot in R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With