R语言 如何在gganimate上添加交点?

bkhjykvo  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(153)

我正在设置两条曲线的动画,其中一条曲线的斜率发生变化,我想在动画中显示每个状态下不断变化的交点。我知道交点在哪里,但不知道如何将它们包含在每个州的图中。
我尝试为每个状态的交叉点添加一个单独的transition_manual,但它只显示了那个,而不是第二个transition。

library(tidyverse)
library(gganimate)

tbl <- tibble(x = seq(-8, 8, by = .01),
             A_1 = 4*x,
             B_1 = x^2,
             A_2 = 3*x,
             B_2 = x^2,
             A_3 = 2*x,
             B_3 = x^2,
             A_4 = x,
             B_4 = x^2,
             A_5 = 0*x,
             B_5 = x^2) %>%
 gather(group, density, A_1:B_5) %>%
 separate(group, c("group", "type"), sep = "_") %>%
 mutate(type = as.numeric(type)) %>%
 mutate(Title = case_when(
   type == 1 ~ "A = 0, B = 4",
   type == 2 ~ "A = 0, B = 3",
   type == 3 ~ "A = 0, B = 2",
   type == 4 ~ "A = 0, B = 1",
   TRUE ~ "A = B = 0"
 ))

  ggplot(tbl) + geom_line(mapping = aes(x = x, y = density, colour = group)) +
 transition_states(Title, transition_length = .5, state_length = 2, wrap = TRUE) +
 labs(title = '{closest_state}') + ylab("f(x)")

这基本上是我想要的方式,除了不显示交点。

2o7dmzc5

2o7dmzc51#

这里有一个方法使用手工计算的交叉点。在这种情况下,它依赖于在计算的值之间存在精确的交集,但是它可以被修改以找到最接近的匹配。

intersects <- tbl %>%
  spread(group, density) %>%
  mutate(var = A - B) %>%
  # group_by(Title) %>%       # Alternative: find top 2 by Title
  # top_n(2, -abs(var)) %>%   # Alternative: find top 2 by Title 
  #                           # (Won't work in some edge cases...)
  filter(var == 0) %>%  # presumes exact intersection exists in rows
  mutate(intersect = TRUE) %>%
  select(x, type, Title, density = A, intersect)

tbl2 <- tbl %>%
  left_join(intersects)

ggplot(tbl2, aes(x, density, colour = group)) + 
  geom_line() +
  geom_point(data = tbl2 %>% filter(intersect)) +
  transition_states(Title, transition_length = .5, state_length = 2, wrap = TRUE) +
  labs(title = '{closest_state}') + ylab("f(x)")

umuewwlo

umuewwlo2#

这里的想法是,你需要添加一个列,它有每个帧的交集(这里即。Title)。

library(tidyverse)
library(gganimate)

#data:
tbl <- tibble(x = seq(-8, 8, by = .01), 
              A_1 = 4*x, B_1 = x^2, A_2 = 3*x, B_2 = x^2, A_3 = 2*x, B_3 = x^2,
              A_4 = x, B_4 = x^2, A_5 = 0*x, B_5 = x^2) %>%
       gather(group, density, A_1:B_5) %>%
       separate(group, c("group", "type"), sep = "_") %>%
       mutate(type = as.numeric(type)) %>%
       mutate(Title = case_when(
                                type == 1 ~ "A = 0, B = 4",
                                type == 2 ~ "A = 0, B = 3",
                                type == 3 ~ "A = 0, B = 2",
                                type == 4 ~ "A = 0, B = 1",
                                TRUE ~ "A = B = 0"))

#pseudo-intersection points for each frame:
intersection <- tbl %>% 
  distinct(Title) %>% 
  mutate(x1 = c(0,0,0,0,0),
         y1 = c(0,0,0,0,0),
         x2 = c(4,3,2,1,NA),
         y2 = c(16,9,5,2,NA))

#plot:
tbl %>% 
  right_join(intersection, by="Title") %>% 
  ggplot(.) + geom_line(mapping = aes(x = x, y = density, colour = group)) +
              geom_point(mapping = aes(x = x1, y = y1, colour = "green")) +
              geom_point(mapping = aes(x = x2, y = y2, colour = "green")) +
              transition_states(Title, transition_length = 0.5, state_length = 2, wrap = TRUE) +
              labs(title = '{closest_state}') + ylab("f(x)")

创建于2019-06-20由reprex package(v0.3.0)

相关问题