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

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

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

问题

以下是代码部分的翻译:

library(gganimate)
library(ggplot2)
library(dplyr)
data_speed <- readRDS("speed.RDS")
animated_histogram <- data_speed %>%
  ggplot(aes(x = fastestLapSpeed)) +
  geom_histogram(fill = "#ff1801") +
  geom_segment(aes(x = avg_speed, xend = avg_speed, y = 0, yend = Inf),
               color = "blue", size = 1, linetype = "") +
  labs(title = "年份: {frame_time}") +
  transition_time(year) +
  ease_aes('linear')

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

library(gganimate)
library(ggplot2)
library(dplyr)
data_speed &lt;- readRDS(&quot;speed.RDS&quot;)
animated_histogram &lt;- data_speed %&gt;%
  ggplot(aes(x = fastestLapSpeed)) +
  geom_histogram(fill = &quot;#ff1801&quot;) +
  geom_segment(aes(x = avg_speed, xend = avg_speed, y = 0, yend = Inf),
               color = &quot;blue&quot;, size = 1, linetype = &quot;&quot;) +
  labs(title = &quot;Year: {frame_time}&quot;) +
  transition_time(year) +
  ease_aes(&#39;linear&#39;)

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

&gt; packageVersion(&quot;gganimate&quot;)
[1] ‘1.0.8’
&gt; packageVersion(&quot;ggplot2&quot;)
[1] ‘3.4.2’
&gt; packageVersion(&quot;dplyr&quot;)
[1] ‘1.1.2’

Thanks in advance!

答案1

得分: 2

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

library(gganimate)
library(ggplot2)
library(dplyr)

tmp <- tempfile(fileext = ".RDS")

download.file("https://github.com/adhok/data_sources_new/raw/main/speed.RDS", tmp)

data_speed <- readRDS(tmp)

data_speed_avg <- data_speed %>%
  summarise(fastestLapSpeed = mean(fastestLapSpeed), .by = year)

ggplot(data_speed, aes(x = fastestLapSpeed)) +
  geom_histogram(fill = "#ff1801") +
  geom_vline(
    data = data_speed_avg, aes(xintercept = fastestLapSpeed),
    color = "blue", linewidth = 1
  ) +
  labs(title = "年份: {frame_time}") +
  transition_time(year) +
  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:

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

tmp &lt;- tempfile(fileext = &quot;.RDS&quot;)

download.file(&quot;https://github.com/adhok/data_sources_new/raw/main/speed.RDS&quot;, tmp)

data_speed &lt;- readRDS(tmp)

data_speed_avg &lt;- data_speed |&gt;
  summarise(fastestLapSpeed = mean(fastestLapSpeed), .by = year)

ggplot(data_speed, aes(x = fastestLapSpeed)) +
  geom_histogram(fill = &quot;#ff1801&quot;) +
  geom_vline(
    data = data_speed_avg, aes(xintercept = fastestLapSpeed),
    color = &quot;blue&quot;, linewidth = 1
  ) +
  labs(title = &quot;Year: {frame_time}&quot;) +
  transition_time(year) +
  ease_aes(&quot;linear&quot;)
#&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:

确定