Filling area under curve based on value

前端 未结 2 889
梦如初夏
梦如初夏 2020-12-01 20:03

We are trying to make an area plot with ggplot2 where the positive areas above the x-axis are one color and the negative areas are another.

Given this data set, I wo

相关标签:
2条回答
  • 2020-12-01 20:22

    Per @baptiste's comment (since deleted) I would say this is the best answer. It is based on this post by Kohske. It adds new x-y pairs to the dataset at zero crossings, and generates the plot below:

    # create some fake data with zero-crossings
    yvals = c(2,2,-1,2,2,2,0,-1,-2,2,-2)
    d = data.frame(x=seq(1,length(yvals)),y=yvals)
    
    rx <- do.call("rbind",
       sapply(1:(nrow(d)-1), function(i){
       f <- lm(x~y, d[i:(i+1),])
       if (f$qr$rank < 2) return(NULL)
       r <- predict(f, newdata=data.frame(y=0))
       if(d[i,]$x < r & r < d[i+1,]$x)
          return(data.frame(x=r,y=0))
        else return(NULL)
     }))
     d2 <- rbind(d,rx)
     ggplot(d2,aes(x,y)) + geom_area(data=subset(d2, y<=0), fill="pink") 
         + geom_area(data=subset(d2, y>=0), fill="lightblue") + geom_point()
    

    Generates the following output: example plot

    0 讨论(0)
  • 2020-12-01 20:22

    I did a quite similar plot using the following easy to understand logic. I created the following two objects for positive and negative values. Note that there is a "very small number" in there to avoid those jumps from a point to another without passing through zeroes.

    pos <- mutate(df, y = ifelse(ROI >= 0, y, 0.0001))
    neg <- mutate(df, y = ifelse(ROI < 0, y, -0.0001))
    

    Then, simply add the geom_areas to your ggplot object:

    ggplot(..., aes(y = y)) + 
      geom_area(data = pos, fill = "#3DA4AB") +
      geom_area(data = neg, fill = "tomato")
    

    Hope it works for you! ;)

    0 讨论(0)
提交回复
热议问题