Highlighting particular regions of a scatterplot in a ggplot

后端 未结 1 1682
陌清茗
陌清茗 2021-02-08 12:25

I need to discuss a scatter plot and would like to refer to particular regions of the plot. Is there any way to \'highlight\' particular sections of the plot? Perhaps with boxes

相关标签:
1条回答
  • 2021-02-08 13:13

    For a single region, it's easiest to use annotate, first with rect, then text:

    p + annotate("rect", xmin=1.5, xmax=2.5, ymin=12500, ymax= 18000, 
                 fill=NA, colour="red") +
        annotate("text", x=1.75, y=17000, label="Region A", size=8)
    

    enter image description here


    For multiple regions, you can put the data into a data frame and use geom_text and geom_rect:

    regions <- data.frame(
      xmin=c(1.5, 1, 0),
      xmax=c(2.5, 2, 1),
      ymin=c(12500, 5000, 0),
      ymax=c(17500, 12500, 5000),
      x   =c(2, 1.5, 0.5),
      y   =c(15000, 7500, 2500),
      lab = paste("Region", LETTERS[1:3])
    )
    
    p + 
      geom_rect(data=regions, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), 
                fill=NA, colour="red") +
      geom_text(data=regions, aes(x=x, y=y, label=lab)) 
    

    enter image description here

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