【问题标题】:Shading only part of the top area under a normal curve仅对正态曲线下的部分顶部区域进行着色
【发布时间】:2021-04-08 06:07:02
【问题描述】:

我正在尝试在正常曲线下的区域中着色,例如在一个标准偏差之间,但不是一直到 x 轴。我需要将其切断以使曲线下方的顶部只有阴影。在下面的代码中,我想在法线曲线下遮蔽区域,但仅在两条虚线之间。任何人都可以帮忙吗?我见过很多例子,其中部分区域的阴影一直向下延伸到 x 轴。但是,我需要一些方法来阻止用户定义的曲线下区域的下部被着色。

library(ggplot2)

#generate a normal distribution plot
Fig1 <- ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
        stat_function(fun = dnorm, args = list(mean=0, sd=1.25),colour = "darkblue", size = 1.25) +
        theme_classic() +
        geom_hline(yintercept = 0.32, linetype = "longdash") +
        geom_hline(yintercept = 0.175, linetype = "longdash")
Fig1

【问题讨论】:

    标签: r ggplot2 area curve shading


    【解决方案1】:

    您可以将geom_polygon 与您的分布数据/下限线的子集一起使用。

    library(ggplot2)
    library(dplyr)
    
    # make data.frame for distribution
    yourDistribution <- data.frame(
      x = seq(-4,4, by = 0.01),
      y = dnorm(seq(-4,4, by = 0.01), 0, 1.25)
    )
    # make subset with data from yourDistribution and lower limit
    upper <- yourDistribution %>% filter(y >= 0.175)
    
    ggplot(yourDistribution, aes(x,y)) +
      geom_line() +
      geom_polygon(data = upper, aes(x=x, y=y), fill="red") +
      theme_classic() +
      geom_hline(yintercept = 0.32, linetype = "longdash") +
      geom_hline(yintercept = 0.175, linetype = "longdash")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 2012-04-20
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多