在此直方图中将计数更改为概率

Change count to probability in this histogram

我正在努力在不弄乱红色区域的情况下将以下直方图中的计数更改为概率。另外,如何将 1,10 与 x 轴上的其余数字对齐?

library(dplyr)
library(tibble)
library(ggplot2)
nrep = 10000
scientific <- function(x){
  ifelse(x==1e+0, "1", ifelse(x==1e+1,"10",parse(text=gsub("[+]", "", gsub("1e+", "10^", scales::scientific_format()(x))))))
}
bw <- 0.05
mx=rf(nrep,5,2)
df = tibble(x = mx)
ggplot(df,aes(x)) + 
geom_histogram(binwidth=bw, color="white", fill = "#1380A1") + 
geom_histogram(data=df %>% filter(x < 10^(-1) + 1.15*bw), binwidth=bw, color="white", fill = "red") +
geom_density(aes(y = bw*after_stat(count)), color="blue") +
scale_x_continuous(trans="log10", breaks = 10^seq(-1, 5, by = 1), labels = scientific)

您需要将图层中的比例从计数更改为密度geom_histogram。在第一个直方图中,您可以使用等同于 after_stat(count/sum(count))/bwafter_stat(density) 来完成。但是,相同的过程在第二个直方图中不起作用,因为当您对数据集进行子集化时 sum(count) 是不同的。如果这样做,第二个直方图将采用不同的比例。

library(dplyr)
library(tibble)
library(ggplot2)
nrep = 10000
scientific <- function(x){
  ifelse(x==1e+0, "1", ifelse(x==1e+1,"10",parse(text=gsub("[+]", "", gsub("1e+", "10^", scales::scientific_format()(x))))))
}
bw <- 0.05
mx=rf(nrep,5,2)

df = tibble(x = mx) 
pdf <- df %>% filter(x < 10^(-1) + 1.15*bw)
ggplot() + 
  geom_histogram(data = df,
                 aes(x = x, y = after_stat(count/sum(count)/bw),
                 binwidth=bw, color="white", fill = "#1380A1") + 
  geom_histogram(data = pdf, 
                 aes(x = x, y = after_stat(count/sum(count)/bw),
                 binwidth=bw, color="white", fill = "red") +
  geom_density(data = df, 
               aes(x = x), color="blue") +
  scale_x_continuous(trans="log10", breaks = 10^seq(-1, 5, by = 1), 
                     labels = scientific)

因此,需要从第一个直方图中计算同分母的密度,定义为nrep

library(dplyr)
library(tibble)
library(ggplot2)
nrep = 10000
scientific <- function(x){
  ifelse(x==1e+0, "1", ifelse(x==1e+1,"10",parse(text=gsub("[+]", "", gsub("1e+", "10^", scales::scientific_format()(x))))))
}
bw <- 0.05
mx=rf(nrep,5,2)

df = tibble(x = mx) 
pdf <- df %>% filter(x < 10^(-1) + 1.15*bw)
ggplot() + 
  geom_histogram(data = df,
                 aes(x = x, y = after_stat(density)),
                 binwidth=bw, color="white", fill = "#1380A1") + 
  geom_histogram(data = pdf, 
                 aes(x = x, y = after_stat(count/nrep)/bw),
                 binwidth=bw, color="white", fill = "red") +
  geom_density(data = df, 
               aes(x = x), color="blue") +
  scale_x_continuous(trans="log10", breaks = 10^seq(-1, 5, by = 1), 
                     labels = scientific)