如何在R ggplot中动态绘制线段(geom_segment)?

4

给定一组固定的点,我可以绘制它们,并添加显式代码以使用geom_segment连接其中的一些点。如果我有一个单独的数据源,其中包含所有线段的坐标,是否有一种方法在同一图中为这些线段添加循环?

    ggplot() + 
      geom_point(data=cm,mapping = aes(x, y)) +
      # connect every two paired assets based on separate data source
      geom_segment (mapping=aes(x=10,y=10,xend=100,yend=100), arrow=arrow(angle = 8,type ="closed",length = unit(0.10, "inches")), 
size=0.2, linetype=1, color="#cccccc")
+ geom_segment ( ...

数据集:

x,y
10.0, 10.0
100.0, 100.0
...

分段:

x,y,x2,y2
10.0, 10.0, 100.0,100.0
...
1个回答

7

您可以为每个几何图形传递不同的数据源。 在这种情况下:

ggplot() + 
      geom_point(data = cm, aes(x, y)) +
      geom_segment(data = segment, aes(x = x, y = y, xend = x2, yend = y2),
                   arrow = arrow(angle = 8,type = "closed",length = unit(0.10, "inches")), 
                   size = 0.2, 
                   linetype = 1,  
                   color = "#cccccc")

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