如何在R中使箭头动画化

tquggr8v  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(95)

我怎样才能使箭头从一端移动到另一端。我想表现一种能量流。

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

# Add lines connecting the circles
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

输出:两条线段和一个箭头

hts6caw3

hts6caw31#

使用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)

相关问题