4

I am plotting a 4 dimensional data set. Beyond the x-axis and y-axis, I want to represent the 3rd and the 4th dimension by rectangles of different width and height. Can I do this with ggplot? Thanks.

enter image description here

3
  • 2
    Can you provide a reproducible example and detail the expected plot? Commented Feb 6, 2013 at 2:01
  • 1
    @agstudy As I understand it, it's basically an x-y scatterplot where each point is a rectangle. The height and width of these rectangle points are mapped to two other variables. Getting the ratios right will be a nuisance and some data to work with would certainly speed things up. Commented Feb 6, 2013 at 2:16
  • @sebastian-c Yes, exactly. I've updated a draft. Commented Feb 6, 2013 at 2:31

2 Answers 2

8

Here is one approach:

dd <- data.frame(x = (x <- 1:10), 
                 y = x + rnorm(10), width = runif(10,1,2), height = runif(10,1,2))

ggplot(data = dd) + 
  geom_rect(aes(xmax = x + width/2, xmin = x - width/2, 
                ymax = y + height/2, ymin = y - height/2), 
            alpha =0.2, color = rgb(0,114,178, maxColorValue=256), 
            fill = rgb(0,114,178, maxColorValue=256)) + 
  coord_fixed() + 
  theme_bw()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

4

You can try something like this. I use

  1. geom_point with shape =0 to simulate rectangle
  2. geom_rect to create ractangle centered around the points

here my data (it would be better to provide some data)

d=data.frame(x=seq(1,10), 
             y=seq(1,10), 
             width=rep(c(0.1,0.5),each =5), 
             height=rep(c(0.8,0.9,0.4,0.6,0.7),each =2)) 

ggplot(data = d) + 
  geom_rect(aes(xmax = x + width, xmin = x-width, 
                ymax = y+height, ymin = y - height), 
            color = "black", fill = NA) + 
  geom_point(mapping=aes(x=x, y=y,size=height/width),
            color='red',shape=0)+
  theme_bw()

enter image description here

1 Comment

Can I have the legend of both width and height?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.