ggplot2 shade area under density curve by group

前端 未结 3 1791
深忆病人
深忆病人 2020-11-29 10:28

I have this dataframe:

set.seed(1)
x <- c(rnorm(50, mean = 1), rnorm(50, mean = 3))
y <- c(rep(\"site1\", 50), rep(\"site2\", 50))
xy <- data.frame(         


        
3条回答
  •  长情又很酷
    2020-11-29 10:47

    The problem with @jlhoward's solution is that you need to manually add goem_ribbon for each group you have. I wrote my own ggplot stat wrapper following this vignette. The benefit of this is that it automatically works with group_by and facet and you don't need to manually add geoms for each group.

    StatAreaUnderDensity <- ggproto(
      "StatAreaUnderDensity", Stat,
      required_aes = "x",
      compute_group = function(data, scales, xlim = NULL, n = 50) {
        fun <- approxfun(density(data$x))
        StatFunction$compute_group(data, scales, fun = fun, xlim = xlim, n = n)
      }
    )
    
    stat_aud <- function(mapping = NULL, data = NULL, geom = "area",
                        position = "identity", na.rm = FALSE, show.legend = NA, 
                        inherit.aes = TRUE, n = 50, xlim=NULL,  
                        ...) {
      layer(
        stat = StatAreaUnderDensity, data = data, mapping = mapping, geom = geom, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes,
        params = list(xlim = xlim, n = n, ...))
    }
    

    Now you can use stat_aud function just like other ggplot geoms.

    set.seed(1)
    x <- c(rnorm(500, mean = 1), rnorm(500, mean = 3))
    y <- c(rep("group 1", 500), rep("group 2", 500))
    t_critical = 1.5
    
    tibble(x=x, y=y)%>%ggplot(aes(x=x,color=y))+
      geom_density()+
      geom_vline(xintercept = t_critical)+
      stat_aud(geom="area",
               aes(fill=y),
               xlim = c(0, t_critical), 
                  alpha = .2)
    

    tibble(x=x, y=y)%>%ggplot(aes(x=x))+
      geom_density()+
      geom_vline(xintercept = t_critical)+
      stat_aud(geom="area",
               fill = "orange",
               xlim = c(0, t_critical), 
                  alpha = .2)+
      facet_grid(~y)
    

提交回复
热议问题