2013-02-06 82 views
4

我正在绘制一个4维数据集。除了x轴和y轴,我想通过不同宽度和高度的矩形表示第3和第4维。我可以用ggplot做到这一点吗?谢谢。如何为ggplot中的每个点使用不同的形状

enter image description here

+2

您能否提供找一个可重复的例子,并详细说明预期的情节? – agstudy

+1

@agstudy据我所知,它基本上是一个x-y散点图,每个点都是一个矩形。这些矩形点的高度和宽度被映射到两个其他变量。正确的比例将是一个麻烦,一些数据的工作肯定会加快速度。 –

+0

@ sebastian-c是的,正好。我已经更新了一份草稿。 – ziyuang

回答

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. geom_point与形状= 0,模拟矩形
  2. geom_rect创建ractangle点

我在这里的数据(这将是更好地提供一些数据)为中心

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

+0

我可以拥有宽度和高度的图例吗? – ziyuang

相关问题