How to use different shapes for every point in ggplot

自作多情 提交于 2019-12-21 05:25:10

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/14720313/how-to-use-different-shapes-for-every-point-in-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!