0

I have some items that have different eligibility criteria - specifically in this example two variables each with a min and max the values are allowed to take. I would like to see the coverage of the products by plotting rectangles for each product on a chart that shows the area between the mins and maxs.

How would you go about

  • converting the records most elegantly to that required by geom_polygon() and
  • ensuring the shapes produced appear as rectangles

Example

library(data.table)
library(ggplot2)
df<-data.table(Product=letters[1:10], minX=1:10, maxX=5:14, minY= 10:1, maxY=14:5)

df.t<-data.table(rbind( df[,list(Product,X=minX,Y=minY)],
       df[,list(Product,X=minX,Y=maxY)],
       df[,list(Product,X=maxX,Y=minY)],
       df[,list(Product,X=maxX,Y=maxY)]))[
      order(Product,X,Y)]

ggplot(df.t,aes(x=X,y=Y,group=Product,fill=Product))+geom_polygon()

Result

NB In this reduced example there are only two criteria, however I have a range of criteria columns and would not want to repeat the exercise above for different combinations.

1 Answer 1

2

Use your original data frame df and then geom_rect() as you already have minimal and maximal values for the x and y.

ggplot(df,aes(xmin=minX,xmax=maxX,ymin=minY,ymax=maxY,fill=Product))+geom_rect()

enter image description here

1
  • D'oh, deceptively simple, shoulda RTFMd more Feb 28, 2014 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.