gpt4 book ai didi

r - ggplot : Fill small area under normal curve: remove "joining" area

转载 作者:行者123 更新时间:2023-12-04 23:37:10 24 4
gpt4 key购买 nike

我想在曲线下填充一小块区域。但是,带状几何图形将分布的两个“部分”连接起来。

library(tidyverse)
density(rnorm(1000, 0, 1)) %$%
data.frame(x=x, y=y) %>%
mutate(area = dplyr::between(x, 1.5, 2.6)) %>%
ggplot() +
geom_ribbon(aes(x = x, ymin = 0, ymax = y, fill = area))

enter image description here

我相信避免这种行为的方法之一是将分布分成三个不同的部分,并用相同的颜色填充其中的两个部分。但是,我正在寻找一种更整洁优雅的方式。

最佳答案

问题是红色丝带在蓝色区域内插值的方式,其中没有红色值,因此绘制一条直线到下一个红色点。您可以通过先绘制整个功能区来解决此问题,而不考虑 area ,然后在顶部绘制子集:

library(tidyverse)
density(rnorm(1000, 0, 1)) %$%
data.frame(x=x, y=y) %>%
mutate(area = dplyr::between(x, 1.5, 2.6)) %>%
ggplot(aes(x = x, ymin = 0, ymax = y)) +
geom_ribbon(aes(fill = "Outside")) +
geom_ribbon(aes(fill = "Inside"), data = function(df) df %>% filter(area))

结果:

enter image description here

关于r - ggplot : Fill small area under normal curve: remove "joining" area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50266468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com