将geom_point的图例与geom_segment分开

时间:2015-04-17 22:50:44

标签: r ggplot2

我想通过ggplot和grid获得轨迹图 所以我们假设 d 是数据点协调的数据框

             x             y   z class  id
1  -0.13194352 -0.0469286036 pre   t2d X12
2  -0.26485347 -0.0146133068 pre   ngt X23
3   0.12347026  0.0304764591 pre   ngt X25
4   0.10687495  0.0284547618 pre   t2d X28
5   0.20315176  0.0395012953 pre   t2d F11
6  -0.18513494 -0.0722370495 pre   t2d E02
7   0.14843496  0.0353464437 pre   t2d E06
8  -0.04834839  0.0052027731  3m   t2d    
9  -0.10649088 -0.0088773648  3m   ngt    
10  0.09479728  0.0618322660  3m   ngt    
11  0.09999894 -0.0049132977  3m   t2d    
12  0.08994520  0.0482155264  3m   t2d    
13 -0.02253838 -0.0222946653  3m   t2d    
14 -0.10736377 -0.0791652377  3m   t2d    
15 -0.02782085  0.0112260892  1y   t2d    
16  0.01603584  0.0056072540  1y   ngt    
17  0.01482530  0.0165045607  1y   ngt    
18  0.03249576 -0.0189961940  1y   t2d    
19 -0.01456136 -0.0019834984  1y   t2d    
20 -0.03141701 -0.0003763406  1y   t2d    
21  0.01044232 -0.0119818709  1y   t2d   

trans 是另一个存储轨迹的数据框

    id      x.from       y.from        x.to          y.to cond
1  E02 -0.18513494 -0.072237050 -0.02253838 -0.0222946653  t2d
2  E06  0.14843496  0.035346444 -0.10736377 -0.0791652377  t2d
3  F11  0.20315176  0.039501295  0.08994520  0.0482155264  t2d
4  X12 -0.13194352 -0.046928604 -0.04834839  0.0052027731  t2d
5  X23 -0.26485347 -0.014613307 -0.10649088 -0.0088773648  ngt
6  X25  0.12347026  0.030476459  0.09479728  0.0618322660  ngt
7  X28  0.10687495  0.028454762  0.09999894 -0.0049132977  t2d
8  E02 -0.02253838 -0.022294665 -0.03141701 -0.0003763406  t2d
9  E06 -0.10736377 -0.079165238  0.01044232 -0.0119818709  t2d
10 F11  0.08994520  0.048215526 -0.01456136 -0.0019834984  t2d
11 X12 -0.04834839  0.005202773 -0.02782085  0.0112260892  t2d
12 X23 -0.10649088 -0.008877365  0.01603584  0.0056072540  ngt
13 X25  0.09479728  0.061832266  0.01482530  0.0165045607  ngt
14 X28  0.09999894 -0.004913298  0.03249576 -0.0189961940  t2d

我想绘制轨迹

  p = ggplot( d, aes( x=x, y=y))
  p = p + geom_point(aes(color = class), size = 2)
  p = p + geom_text(aes(label=id),hjust=0, vjust=0)
  p = p + geom_segment( data=trans, 
                        aes( x=x.from, y=y.from, xend=x.to, yend=y.to, color = id, alpha = 1, size = 20), 
                        lwd=1, lineend="butt", 
                        arrow=arrow(angle = 20, length = unit(0.4, "cm"), type = "open"), 
                        alpha = 1)
  p

enter image description here

我想在一个单独的图例中有“ngt”和“t2d”,并用形状而不是颜色显示它们;然而,ggplot,混合传说...有没有办法制作两个传说,1个指南“ngt”和“t2d”,另一个是其余的?

1 个答案:

答案 0 :(得分:0)

你想要的是这样的吗?这些点对应于课程中的两个级别,scale_shape_manual()可让您自定义其形状和图例外观。

ggplot(d, aes( x=x, y=y)) + geom_point(aes(shape = class), size = 4) +
  scale_shape_manual(values = c(22, 15)) +
  geom_text(aes(label=id), hjust=0, vjust=0) +
  geom_segment(data=trans, 
                      aes( x=x.from, y=y.from, xend=x.to, yend=y.to, color = id, alpha = 1, size = 20), 
                      lwd=1, lineend="butt", arrow=arrow(angle = 20, length = unit(0.4, "cm"), type = "open"), 
                      alpha = 1)

enter image description here