ggplot2 - Shade area above line

前端 未结 3 1981
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 22:53

I have some data that is constrained below a 1:1 line. I would to demonstrate this on a plot by lightly shading the area ABOVE the line, to draw the attention of the viewer

3条回答
  •  悲哀的现实
    2020-12-13 23:40

    As far as I know there is no other way other than creating a polygon with alpha-blended fill. For example:

    df <- data.frame(x=1, y=1)
    df_poly <- data.frame(
        x=c(-Inf, Inf, -Inf),
        y=c(-Inf, Inf, Inf)
    )
    
    ggplot(df, aes(x, y)) + 
        geom_blank() + 
        geom_abline(slope=1, intercept=0) + 
        geom_polygon(data=df_poly, aes(x, y), fill="blue", alpha=0.2) +
    

    enter image description here

提交回复
热议问题