Adding multiple shadows/rectangles to ggplot2 graph

后端 未结 2 688
后悔当初
后悔当初 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

提交回复
热议问题