gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:02:18 28 4
gpt4 key购买 nike

给定一组固定的点,我可以绘制它们并添加显式代码以将其中一些点与 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
...

最佳答案

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

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

关于r - 如何在 R ggplot 中动态绘制线段(geom_segment)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42998145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com