且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为ggplot中的每个点使用不同的形状

更新时间:2022-10-25 19:21:59

以下是一种方法:

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


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.

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