如何在R中使箭头动画化

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

How to make the arrows animate in R

问题

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

plot <- ggplot(combined_total_active_2, aes(x = 0, y = 0)) +
  coord_fixed() +
  theme_void() +
  theme(panel.background = element_rect(fill = "#11141a"),
        plot.background = element_rect(fill = "#11141a"))

# 添加连接圆圈的线段
plot <- plot +
  geom_curve(aes(x = 0, y = 0, xend = -1, yend = 0),
             curvature = 0, arrow = arrow(length = unit(0.21, "inches")),
             color = "#ffc559", size = 1.2, alpha = 0.6) +

  geom_segment(aes(x = -3, y = 0, xend = 3, yend = 0), color = "#ffc559",
               size = 1.5, lineend = "round") +
  geom_segment(aes(x = 0, y = -3, xend = 0, yend = 3), color = "#ffc559",
               size = 1.5, lineend = "round")

plot

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

英文:

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


plot &lt;- ggplot(combined_total_active_2, aes(x = 0, y = 0)) +
  coord_fixed()+
  theme_void()+
  theme(panel.background = element_rect(fill = &quot;#11141a&quot;),
        plot.background = element_rect(fill = &quot;#11141a&quot;))

# Add lines connecting the circles
plot &lt;- plot +
  
  geom_curve(aes(x = 0, y = 0, xend = -1, yend = 0),
             curvature = 0, arrow = arrow(length = unit(0.21, &quot;inches&quot;)), 
             color = &quot;#ffc559&quot;, size = 1.2, alpha = 0.6)+

  geom_segment(aes(x = -3, y = 0, xend = 3, yend = 0), color = &quot;#ffc559&quot;,
                               size = 1.5, lineend = &quot;round&quot;) +
  geom_segment(aes(x = 0, y = -3, xend = 0, yend = 3), color = &quot;#ffc559&quot;,
                               size = 1.5, lineend = &quot;round&quot;)

plot

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

答案1

得分: 1

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

library(gganimate)

p <- ggplot(data.frame(val = seq(3, -3, -0.1), time = 1:61)) +
  geom_segment(arrow = arrow(length = unit(0.21, "inches")), 
               color = "#ffc559", linewidth = 1.2, alpha = 0.6,
               aes(x = 3, y = 0, xend = val, yend = 0)) +
  geom_hline(color = "#ffc559", linewidth = 1.2, yintercept = 0) +
  geom_vline(color = "#ffc559", linewidth = 1.2, xintercept = 0) +
  coord_fixed(xlim = c(-3, 3), ylim = c(-3, 3)) +
  theme_void() +
  theme(panel.background = element_rect(fill = "#11141a"),
        plot.background = element_rect(fill = "#11141a")) +
  transition_manual(time)

animate(p, fps = 61)

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

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

animate(p, fps = 61)

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

英文:

Using gganimate you could do:

library(gganimate)

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

animate(p, fps = 61)

如何在R中使箭头动画化


EDIT

To change to a filled circle we can just do

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

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:

确定