用于与过渡状态相关的动态更改自定义绘图注释的gganimate。

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

Animate / dynamically change custom plot annotation gganimate with relation to transition states

问题

如何在gganimate中动态更改自定义绘图注释,与转换状态相关,类似于https://stackoverflow.com/questions/37397303/change-label-of-gganimate-frame-title,但注释应放置在绘图面板内。https://stackoverflow.com/questions/54724719/animated-barplot-via-gganimate-annotate-box-with-changing-text-below-plot类似,但这里的注释更简单,因为它直接与转换状态相关。

library(gganimate)
#> 加载所需的包: ggplot2

df <- data.frame(y = letters[1:6], x = c(1:6))

p <-
  ggplot(df, aes(x, y)) +
  geom_col() 

p_anim <- p + transition_states(y) + shadow_mark()

## 所期望的结果是一个根据转换状态变化的注释。
英文:

How can I dynamically change a custom plot annotation in gganimate with relation to transition states, similar to https://stackoverflow.com/questions/37397303/change-label-of-gganimate-frame-title, but the annotation should be placed within the plot panel. https://stackoverflow.com/questions/54724719/animated-barplot-via-gganimate-annotate-box-with-changing-text-below-plot is similar, but the annotation here is simpler, as it directly relates to the transition states.

library(gganimate)
#&gt; Loading required package: ggplot2

df &lt;- data.frame(y = letters[1:6], x = c(1:6))

p &lt;-
  ggplot(df, aes(x, y)) +
  geom_col() 

p_anim &lt;- p + transition_states(y) + shadow_mark()

## the desired outcome is an annotation which changes according to the transition state. 

答案1

得分: 3

你可以按照链接问题中标题的方法进行操作,但是你需要使用一个通常很少用到的注释,具体是"tag"。你可以将该标签相对于主题中的面板进行定位。

library(gganimate)
#&gt; 加载所需的包: ggplot2

df &lt;- data.frame(y = letters[1:6], x = c(1:6))

p &lt;-
  ggplot(df, aes(x, y)) +
  geom_col() +
  labs(tag = &quot;Animate tag = {closest_state}&quot;) +
  theme( 
    ## 该标签允许相对于面板进行定位
    plot.tag.position = c(.7, .2),
    ## 使得标签始终位于相同位置
    plot.tag = element_text(hjust = 0)
  ) 

p_anim &lt;- p + transition_states(y) + shadow_mark()

animate(p_anim)

用于与过渡状态相关的动态更改自定义绘图注释的gganimate。<!-- -->

<sup>创建于2023-03-07,使用 reprex v2.0.2。</sup>

英文:

You can go down the same route as with the title in the linked question, but you will need to make use of an otherwise rarely used annotation, specifically the "tag". You can position the tag relative to your panel in theme.

library(gganimate)
#&gt; Loading required package: ggplot2

df &lt;- data.frame(y = letters[1:6], x = c(1:6))

p &lt;-
  ggplot(df, aes(x, y)) +
  geom_col() +
  labs(tag = &quot;Animate tag = {closest_state}&quot;) +
  theme( 
    ## the tag allows positioning relative to the panel 
    plot.tag.position = c(.7, .2),
    ## so that the tag is always at the same position
    plot.tag = element_text(hjust = 0)
  ) 

p_anim &lt;- p + transition_states(y) + shadow_mark()

animate(p_anim)

用于与过渡状态相关的动态更改自定义绘图注释的gganimate。<!-- -->

<sup>Created on 2023-03-07 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月7日 22:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663460.html
匿名

发表评论

匿名网友

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

确定