如何在R中使箭头动画化

huangapple go评论109阅读模式
英文:

How to make the arrows animate in R

问题

如何使箭头从一端移动到另一端。我想表示能量流动。

  1. plot <- ggplot(combined_total_active_2, aes(x = 0, y = 0)) +
  2. coord_fixed() +
  3. theme_void() +
  4. theme(panel.background = element_rect(fill = "#11141a"),
  5. plot.background = element_rect(fill = "#11141a"))
  6. # 添加连接圆圈的线段
  7. plot <- plot +
  8. geom_curve(aes(x = 0, y = 0, xend = -1, yend = 0),
  9. curvature = 0, arrow = arrow(length = unit(0.21, "inches")),
  10. color = "#ffc559", size = 1.2, alpha = 0.6) +
  11. geom_segment(aes(x = -3, y = 0, xend = 3, yend = 0), color = "#ffc559",
  12. size = 1.5, lineend = "round") +
  13. geom_segment(aes(x = 0, y = -3, xend = 0, yend = 3), color = "#ffc559",
  14. size = 1.5, lineend = "round")
  15. plot

输出:
两条线段和1个箭头
如何在R中使箭头动画化

英文:

How can I make the arrow move from one end to other. I want to represent an energy flow.

  1. plot &lt;- ggplot(combined_total_active_2, aes(x = 0, y = 0)) +
  2. coord_fixed()+
  3. theme_void()+
  4. theme(panel.background = element_rect(fill = &quot;#11141a&quot;),
  5. plot.background = element_rect(fill = &quot;#11141a&quot;))
  6. # Add lines connecting the circles
  7. plot &lt;- plot +
  8. geom_curve(aes(x = 0, y = 0, xend = -1, yend = 0),
  9. curvature = 0, arrow = arrow(length = unit(0.21, &quot;inches&quot;)),
  10. color = &quot;#ffc559&quot;, size = 1.2, alpha = 0.6)+
  11. geom_segment(aes(x = -3, y = 0, xend = 3, yend = 0), color = &quot;#ffc559&quot;,
  12. size = 1.5, lineend = &quot;round&quot;) +
  13. geom_segment(aes(x = 0, y = -3, xend = 0, yend = 3), color = &quot;#ffc559&quot;,
  14. size = 1.5, lineend = &quot;round&quot;)
  15. plot

Output:
Two line segments and 1 arrow
如何在R中使箭头动画化

答案1

得分: 1

使用gganimate,您可以执行以下操作:

  1. library(gganimate)
  2. p <- ggplot(data.frame(val = seq(3, -3, -0.1), time = 1:61)) +
  3. geom_segment(arrow = arrow(length = unit(0.21, "inches")),
  4. color = "#ffc559", linewidth = 1.2, alpha = 0.6,
  5. aes(x = 3, y = 0, xend = val, yend = 0)) +
  6. geom_hline(color = "#ffc559", linewidth = 1.2, yintercept = 0) +
  7. geom_vline(color = "#ffc559", linewidth = 1.2, xintercept = 0) +
  8. coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3)) +
  9. theme_void() +
  10. theme(panel.background = element_rect(fill = "#11141a"),
  11. plot.background = element_rect(fill = "#11141a")) +
  12. transition_manual(time)
  13. animate(p, fps = 61)

要更改为填充的圆圈,只需执行以下操作:

  1. p <- ggplot(data.frame(val = seq(3, -3, -0.1), time = 1:61)) +
  2. geom_point(aes(x = val, y = 0), size = 8, color = "#ffc559") +
  3. geom_hline(color = "#ffc559", linewidth = 1.2, yintercept = 0) +
  4. geom_vline(color = "#ffc559", linewidth = 1.2, xintercept = 0) +
  5. coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3)) +
  6. theme_void() +
  7. theme(panel.background = element_rect(fill = "#11141a"),
  8. plot.background = element_rect(fill = "#11141a")) +
  9. transition_manual(time)
  10. animate(p, fps = 61)

您提供的R代码已经被翻译成中文。

英文:

Using gganimate you could do:

  1. library(gganimate)
  2. p &lt;- ggplot(data.frame(val = seq(3, -3, -0.1), time = 1:61)) +
  3. geom_segment(arrow = arrow(length = unit(0.21, &quot;inches&quot;)),
  4. color = &quot;#ffc559&quot;, linewidth = 1.2, alpha = 0.6,
  5. aes(x = 3, y = 0, xend = val, yend = 0)) +
  6. geom_hline(color = &quot;#ffc559&quot;, linewidth = 1.2, yintercept = 0) +
  7. geom_vline(color = &quot;#ffc559&quot;, linewidth = 1.2, xintercept = 0) +
  8. coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3)) +
  9. theme_void() +
  10. theme(panel.background = element_rect(fill = &quot;#11141a&quot;),
  11. plot.background = element_rect(fill = &quot;#11141a&quot;)) +
  12. transition_manual(time)
  13. animate(p, fps = 61)

如何在R中使箭头动画化


EDIT

To change to a filled circle we can just do

  1. p &lt;- ggplot(data.frame(val = seq(3, -3, -0.1), time = 1:61)) +
  2. geom_point(aes(x = val, y = 0), size = 8, color = &quot;#ffc559&quot;) +
  3. geom_hline(color = &quot;#ffc559&quot;, linewidth = 1.2, yintercept = 0) +
  4. geom_vline(color = &quot;#ffc559&quot;, linewidth = 1.2, xintercept = 0) +
  5. coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3)) +
  6. theme_void() +
  7. theme(panel.background = element_rect(fill = &quot;#11141a&quot;),
  8. plot.background = element_rect(fill = &quot;#11141a&quot;)) +
  9. transition_manual(time)
  10. animate(p, fps = 61)

如何在R中使箭头动画化

huangapple
  • 本文由 发表于 2023年6月19日 05:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定