英文:
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 <- 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 = "Year: {frame_time}") +
transition_time(year) +
ease_aes('linear')
animate(animated_histogram, fps = 30, duration = 10, width = 500, height = 250)
Based on the above code , I get the following plot.
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
> packageVersion("gganimate")
[1] ‘1.0.8’
> packageVersion("ggplot2")
[1] ‘3.4.2’
> packageVersion("dplyr")
[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)
#> Loading required package: ggplot2
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 = "Year: {frame_time}") +
transition_time(year) +
ease_aes("linear")
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论