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

4
我正在绘制一个4维数据集。除了x轴和y轴之外,我想通过不同宽度和高度的矩形来表示第三维和第四维。我能用ggplot做到吗?谢谢。

2
你能提供一个可重现的例子并详细说明预期的图形吗? - agstudy
1
据我理解,这基本上是一个x-y散点图,其中每个点都是一个矩形。这些矩形点的高度和宽度映射到另外两个变量。正确地获取比例将会很麻烦,有一些数据可以加速工作。 - sebastian-c
@sebastian-c 是的,没错。我已经更新了草稿。 - Ziyuan
2个回答

8
这里有一种方法:

以下是一个示例:

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


4
您可以尝试像这样做。我使用:
  1. 形状为0的geom_point模拟矩形
  2. geom_rect创建以点为中心的矩形
这是我的数据(提供一些数据会更好):
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


我可以同时获取宽度和高度的图例吗? - Ziyuan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接