2

I have a dataframe that I want to present in 4 facets (by ID), and for each facet, highlight the data with a rectangle.

My data is:

dt<-data.frame(ID=c(rep(c("1","2","3","4"),2)),var=c(480,1180,170,130,500,1180,1450,750),
                 Method=c(rep(c("A","B"),each=4)))

All the other related posts I can find make reference to creating a new dataframe with the infomation for each rectangle as a separate row. As such I have created the following dataframe:

rect<-data.frame(xmin = c(0.8, 0.8,0.8,0.8), xmax = c(2.2, 2.2, 2.2, 2.2), 
           ymin = c(430, 1130, 120, 80), ymax = c(550, 1230, 1500, 800), 
           alpha = c(.1, .1, .1, .1),
           fill = c("red", "green","orange","blue"))

My ggplot code is:

ggplot(dt,aes(x=Method,y=var)) +
  geom_point() +
  annotate("rect", xmin = rect$xmin, xmax = rect$xmax, ymin = rect$ymin, ymax = rect$ymax,
           alpha = 0.1, fill = "green")+
  facet_wrap(~ID,ncol=4)+
  theme_bw()

Which gives me the following:Plot with annotate code

The geom_rect option doesn't work for me at all. I just get error messages about mising variables. I've added an ID and Method column to the rect dataframe,but this just throws up the issue of the var variable no tbeing there. I've considered merging it all but I'm not sure that will fix the problem. This is what I've tried using with geom_rect

geom_rect(data=rect, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
          alpha = 0.1,fill = "blue")+

What I want is just one rectangle in each facet, as per the rect dataframe. Not all of them on each dataframe. Ignore the colours for now, if they are all green or blue is fine.

Thanks in advance for your help.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
EB77
  • 31
  • 2

1 Answers1

4

I think the annotate() approach doesn't allow for facet specificity, so geom_rect() is probably the way to go. The two things you'd have to do is to (1) set inherit.aes = FALSE or change the global aes() to the geom_point() layer and (2) add facet information to the rect data.frame.

library(ggplot2)

dt<-data.frame(ID=c(rep(c("1","2","3","4"),2)),var=c(480,1180,170,130,500,1180,1450,750),
               Method=c(rep(c("A","B"),each=4)))

rect<-data.frame(xmin = c(0.8, 0.8,0.8,0.8), xmax = c(2.2, 2.2, 2.2, 2.2), 
                 ymin = c(430, 1130, 120, 80), ymax = c(550, 1230, 1500, 800), 
                 alpha = c(.1, .1, .1, .1),
                 fill = c("red", "green","orange","blue"))

ggplot(dt,aes(x=Method,y=var)) +
  geom_point() +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            alpha = 0.1, fill = "green",
            data = transform(rect, ID = as.character(1:4)),
            inherit.aes = FALSE) +
  facet_wrap(~ID,ncol=4)+
  theme_bw()

Created on 2021-04-12 by the reprex package (v1.0.0)

Small side note, if you want the rectangles to have the fills from your data.frame verbatim, you can use fill = I(fill) in the rectangle aes() (and remove the fill assignment outside the aes).

teunbrand
  • 33,645
  • 4
  • 37
  • 63