Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use different shapes for every point in ggplot

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

like image 657
ziyuang Avatar asked Dec 16 '22 14:12

ziyuang


2 Answers

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

like image 147
orizon Avatar answered Jan 04 '23 22:01

orizon


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

like image 42
agstudy Avatar answered Jan 04 '23 23:01

agstudy