如何确保使用gganimate进行geom_segment()的平滑过渡?

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

How do I ensure a smooth transition for geom_segment() using gganimate?

问题

以下是代码部分的翻译:

  1. library(gganimate)
  2. library(ggplot2)
  3. library(dplyr)
  4. data_speed <- readRDS("speed.RDS")
  5. animated_histogram <- data_speed %>%
  6. ggplot(aes(x = fastestLapSpeed)) +
  7. geom_histogram(fill = "#ff1801") +
  8. geom_segment(aes(x = avg_speed, xend = avg_speed, y = 0, yend = Inf),
  9. color = "blue", size = 1, linetype = "") +
  10. labs(title = "年份: {frame_time}") +
  11. transition_time(year) +
  12. ease_aes('linear')
  13. animate(animated_histogram, fps = 30, duration = 10, width = 500, height = 250)

希望这对你有所帮助!

英文:

I am trying to plot a histogram with a mean vertical line for each year in the data provided . The data can be found here

The code is as follows

  1. library(gganimate)
  2. library(ggplot2)
  3. library(dplyr)
  4. data_speed &lt;- readRDS(&quot;speed.RDS&quot;)
  5. animated_histogram &lt;- data_speed %&gt;%
  6. ggplot(aes(x = fastestLapSpeed)) +
  7. geom_histogram(fill = &quot;#ff1801&quot;) +
  8. geom_segment(aes(x = avg_speed, xend = avg_speed, y = 0, yend = Inf),
  9. color = &quot;blue&quot;, size = 1, linetype = &quot;&quot;) +
  10. labs(title = &quot;Year: {frame_time}&quot;) +
  11. transition_time(year) +
  12. ease_aes(&#39;linear&#39;)
  13. animate(animated_histogram, fps = 30, duration = 10, width = 500, height = 250)

Based on the above code , I get the following plot.

如何确保使用gganimate进行geom_segment()的平滑过渡?

How do I ensure that the vertical mean line (blue dashed line) transitions smoothly without any breaks in between?

The library versions are as follows

  1. &gt; packageVersion(&quot;gganimate&quot;)
  2. [1] 1.0.8
  3. &gt; packageVersion(&quot;ggplot2&quot;)
  4. [1] 3.4.2
  5. &gt; packageVersion(&quot;dplyr&quot;)
  6. [1] 1.1.2

Thanks in advance!

答案1

得分: 2

也许这已经足够平滑了。首先,我建议使用geom_vline而不是geom_segment。其次,不要像你一样为数据的每一行添加一个段或垂直线,而是使用一个汇总后的数据集来为geom_vline创建一个包含每年一个行的数据集:

  1. library(gganimate)
  2. library(ggplot2)
  3. library(dplyr)
  4. tmp <- tempfile(fileext = ".RDS")
  5. download.file("https://github.com/adhok/data_sources_new/raw/main/speed.RDS", tmp)
  6. data_speed <- readRDS(tmp)
  7. data_speed_avg <- data_speed %>%
  8. summarise(fastestLapSpeed = mean(fastestLapSpeed), .by = year)
  9. ggplot(data_speed, aes(x = fastestLapSpeed)) +
  10. geom_histogram(fill = "#ff1801") +
  11. geom_vline(
  12. data = data_speed_avg, aes(xintercept = fastestLapSpeed),
  13. color = "blue", linewidth = 1
  14. ) +
  15. labs(title = "年份: {frame_time}") +
  16. transition_time(year) +
  17. ease_aes("linear")

图片已经展示在上面的链接中。

英文:

Perhaps this is smooth enough. First, I would suggest to use a geom_vline instead of a geom_segment. Second, instead of adding a segment or vline for each row of your data as you do I use a summarized dataset for the geom_vline which contains one row per year:

  1. library(gganimate)
  2. #&gt; Loading required package: ggplot2
  3. library(ggplot2)
  4. library(dplyr)
  5. tmp &lt;- tempfile(fileext = &quot;.RDS&quot;)
  6. download.file(&quot;https://github.com/adhok/data_sources_new/raw/main/speed.RDS&quot;, tmp)
  7. data_speed &lt;- readRDS(tmp)
  8. data_speed_avg &lt;- data_speed |&gt;
  9. summarise(fastestLapSpeed = mean(fastestLapSpeed), .by = year)
  10. ggplot(data_speed, aes(x = fastestLapSpeed)) +
  11. geom_histogram(fill = &quot;#ff1801&quot;) +
  12. geom_vline(
  13. data = data_speed_avg, aes(xintercept = fastestLapSpeed),
  14. color = &quot;blue&quot;, linewidth = 1
  15. ) +
  16. labs(title = &quot;Year: {frame_time}&quot;) +
  17. transition_time(year) +
  18. ease_aes(&quot;linear&quot;)
  19. #&gt; `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

如何确保使用gganimate进行geom_segment()的平滑过渡?<!-- -->

huangapple
  • 本文由 发表于 2023年7月23日 15:48:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747150.html
匿名

发表评论

匿名网友

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

确定