Suppose I have the dataframe like below:
df <- data.frame(x = runif(100))
df$x2 = df$x*100
cut = quantile(df$x2, 0.75)
df$label = ifelse(df$x2>cut, 1, 0)
x x2 label
1 0.1431888 14.31888 0
2 0.9131599 91.31599 1
3 0.5659831 56.59831 0
4 0.8358059 83.58059 1
5 0.3125397 31.25397 0
6 0.8823542 88.23542 1
The task is:
Firstly, to show the histogram of x
, which can be done using the geom_histogram()
Secondly, in each bin, I want to color the bin by the fraction of label equals 1 in this bin.
I am confused about how to achieve it. Because I need to know the number of 1 in this bin and the number of point in this bin, which is difficult for me how to do it (the binwidth is not fixed). Since I search in the website but only find that the geom_histogram()
color change by the x
, for example in this link .
The output result I want is like this:
:
The image is generated by the following code:
ggplot(df, aes(x = x, fill = ..x..)) + geom_histogram()
But in this example, the color depends on x
in each bin. However, I want the color to depend on the fraction of label equals 1 (the third column) in each bin.