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()
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.