【发布时间】:2020-04-10 06:12:47
【问题描述】:
我正在努力将计数更改为以下直方图中的概率,而不会弄乱红色区域。另外,如何将 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从计数转换为密度,您需要将y = after_stat(density)添加到aes()。然后,您可以使用fill条件突出显示aes()内的部分直方图。见github.com/yutannihilation/gghighlight/issues/… -
@atsyplenkov 谢谢。我编辑了代码。它现在应该可以工作了。