How to use different shapes for each ggplot point

I am drawing a four-dimensional dataset. Besides the x axis and y axis, I want to represent the 3rd and 4th dimensions with rectangles of different widths and heights. Can I do this with ggplot ? Thanks.

enter image description here

+4
source share
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

+8
source

You can try something like this. I use

  • geom_point with shape = 0 to model a rectangle
  • geom_rect to create a rectangle centered around the points.

here is 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

+4
source

All Articles