【问题标题】:How to color two opposite sides of plot?如何为情节的两个相对面着色?
【发布时间】:2023-02-16 03:58:10
【问题描述】:

我正在尝试使用 geom_plot 填充双边假设图下的拒绝区域。

library(ggplot2)
ggplot(data.frame(x = c(0:30)), aes(x = x)) +
  stat_function(fun = dnorm, 
                args = list(mean = 30 * 0.6, 
                            sd = sqrt(30 * 0.6 * (1 - 0.6))),
                aes(col = "H0")) +
  
  geom_area(stat = "function",
            fun = dnorm,
            args = list(mean = 30 * 0.6, 
                        sd = sqrt(30 * 0.6 * (1 - 0.6))),
            aes(fill = "alpha"),
            xlim = c(c(0,13), c(23,30))
            )
  

我尝试给 xlim 两个向量作为参数,但它只接受第一个向量并只填充 x 在 0 到 13 之间的区域,完全忽略第二个向量。从技术上讲,我可以添加第二个 geom_area 两个使其工作,但我觉得这是违反直觉的,它应该只用一个 geom_area 函数就可以了。有任何想法吗?提前致谢!

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    像这样重复 geom_arealapply 怎么样:

    ggplot(data.frame(x = c(0:30)), aes(x = x)) +
      stat_function(fun = dnorm, 
                    args = list(mean = 30 * 0.6, 
                                sd = sqrt(30 * 0.6 * (1 - 0.6))),
                    aes(col = "H0")) +
      
      lapply(list(c(0,13), c(23,30)), function(x) 
        geom_area(stat = "function",
                fun = dnorm,
                args = list(mean = 30 * 0.6, 
                            sd = sqrt(30 * 0.6 * (1 - 0.6))),
                aes(fill = "alpha"),
                xlim = x
      )) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      相关资源
      最近更新 更多