Adding multiple shadows/rectangles to ggplot2 graph

后端 未结 2 676
后悔当初
后悔当初 2020-12-16 12:25

I am trying to add multiple shadows/rectangles over a ggplot2 graph. In this reproducible example, I am only adding 3, but I may need to add up to a hundred using the full

相关标签:
2条回答
  • 2020-12-16 12:57

    it's better to use only one layer, with suitable mapping,

    tempindex <- transform(tempindex, 
                           id = 1:3,
                           tier = c(1,1,2))
    
    
    ggplot(temp, aes(Season,value, color=group)) + 
      geom_rect(data=tempindex, inherit.aes=FALSE,
                aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,
                    group=id, fill = factor(tier)), alpha=0.2)+
      geom_point(size=4, shape=19) +
      scale_color_manual(values=c("red", "gray55"))+
      scale_fill_manual(values=c("green", "blue")) +
      guides(fill="none")
    

    enter image description here

    0 讨论(0)
  • 2020-12-16 13:04

    Another option (more straightforward avoiding creating auxiliary data.frames) could be that one:

    ggplot(tmp, aes(Season,value, color = group)) + geom_point(size = 4, shape = 19) +
      scale_color_manual(values = c("red", "gray55")) + 
      annotate("rect", xmin = c(1947.5, 1950.5, 1957.5), xmax = c(1948.5, 1956.5, 1965.5), 
               ymin = -Inf, ymax = Inf, alpha = .1, fill = c("green", "green", "blue"))
      
    
    0 讨论(0)
提交回复
热议问题