将 gganimate 与 geom_point 和 geom_line 结合使用
Using gganimate with geom_point and geom_line
在这里使用 nfl 数据..第一次尝试 gganimate...尝试使用 geom_point 和 geom_line 但 geom_line 不会出现..
这里是数据框..
week = c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17)
plays= c(9,10,5,3,4,4,3,5,6,5,11,12,6,11,7,3)
mean_epa= c(.67, .27, -.5, -1.09, -.3, .68, -.72, -.32, 1.03, 1.05, .56, .17, -.61, -.05, -.14, 1.5)
CLEdrive1b <- data.frame(week,plays,mean_epa)
在周变量内,缺少 7(因为布朗队没有第 7 周的比赛)...不确定这是否会导致问题?
下面是我的第一次尝试
p <- ggplot(CLEdrive1b, aes(x=as.factor(week),y=mean_epa)) +
theme_minimal() +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5)+
transition_reveal(week)+
geom_text(aes(label = mean_epa))+
xlab("Week") +
ylab("EPA/Play") +
labs(title="Browns Opening Drives",
subtitle = "EPA/Drive by Week",
caption="Data from nflscrapR")+
theme(axis.title = element_text(size = 10),
axis.text = element_text(size = 9),
plot.title = element_text(size = 15),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8))
animate(p)
我试过的替代方法..
pi <- ggplot(CLEdrive1b, aes(week,y=mean_epa)) +
theme_minimal() +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5)+
transition_reveal(week)+
geom_text(aes(label = mean_epa))+
xlab("Week") +
ylab("EPA/Play") +
labs(title="Browns Opening Drives",
subtitle = "EPA/Drive by Week",
caption="Data from nflscrapR")+
theme(axis.title = element_text(size = 10),
axis.text = element_text(size = 9),
plot.title = element_text(size = 15),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8))
animate(pi)
从 x 变量(周)中删除了 as.factor.. 并且 geom_line 出现了!但是 x 轴丢失了我正在寻找的标签并且 geom_text 停止执行上面的方式..
感谢您花时间帮助...
问题在于,当将其转换为因子时,ggplot 不再 "know" 如何对周变量进行分组,并且不再连接观察值。只需将 aes(group = 1)
添加到 geom_line
即可。
library(gganimate)
#> Loading required package: ggplot2
library(ggplot2)
week = c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17)
plays= c(9,10,5,3,4,4,3,5,6,5,11,12,6,11,7,3)
mean_epa= c(.67, .27, -.5, -1.09, -.3, .68, -.72, -.32, 1.03, 1.05, .56, .17, -.61, -.05, -.14, 1.5)
CLEdrive1b <- data.frame(week,plays,mean_epa)
p <- ggplot(CLEdrive1b, aes(x=as.factor(week),y=mean_epa)) +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5, aes(group = 1))+
transition_reveal(week)+
geom_text(aes(label = mean_epa))
animate(p)
由 reprex package (v0.3.0)
于 2020-02-03 创建
在这里使用 nfl 数据..第一次尝试 gganimate...尝试使用 geom_point 和 geom_line 但 geom_line 不会出现..
这里是数据框..
week = c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17)
plays= c(9,10,5,3,4,4,3,5,6,5,11,12,6,11,7,3)
mean_epa= c(.67, .27, -.5, -1.09, -.3, .68, -.72, -.32, 1.03, 1.05, .56, .17, -.61, -.05, -.14, 1.5)
CLEdrive1b <- data.frame(week,plays,mean_epa)
在周变量内,缺少 7(因为布朗队没有第 7 周的比赛)...不确定这是否会导致问题?
下面是我的第一次尝试
p <- ggplot(CLEdrive1b, aes(x=as.factor(week),y=mean_epa)) +
theme_minimal() +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5)+
transition_reveal(week)+
geom_text(aes(label = mean_epa))+
xlab("Week") +
ylab("EPA/Play") +
labs(title="Browns Opening Drives",
subtitle = "EPA/Drive by Week",
caption="Data from nflscrapR")+
theme(axis.title = element_text(size = 10),
axis.text = element_text(size = 9),
plot.title = element_text(size = 15),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8))
animate(p)
我试过的替代方法..
pi <- ggplot(CLEdrive1b, aes(week,y=mean_epa)) +
theme_minimal() +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5)+
transition_reveal(week)+
geom_text(aes(label = mean_epa))+
xlab("Week") +
ylab("EPA/Play") +
labs(title="Browns Opening Drives",
subtitle = "EPA/Drive by Week",
caption="Data from nflscrapR")+
theme(axis.title = element_text(size = 10),
axis.text = element_text(size = 9),
plot.title = element_text(size = 15),
plot.subtitle = element_text(size = 10),
plot.caption = element_text(size = 8))
animate(pi)
从 x 变量(周)中删除了 as.factor.. 并且 geom_line 出现了!但是 x 轴丢失了我正在寻找的标签并且 geom_text 停止执行上面的方式..
感谢您花时间帮助...
问题在于,当将其转换为因子时,ggplot 不再 "know" 如何对周变量进行分组,并且不再连接观察值。只需将 aes(group = 1)
添加到 geom_line
即可。
library(gganimate)
#> Loading required package: ggplot2
library(ggplot2)
week = c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17)
plays= c(9,10,5,3,4,4,3,5,6,5,11,12,6,11,7,3)
mean_epa= c(.67, .27, -.5, -1.09, -.3, .68, -.72, -.32, 1.03, 1.05, .56, .17, -.61, -.05, -.14, 1.5)
CLEdrive1b <- data.frame(week,plays,mean_epa)
p <- ggplot(CLEdrive1b, aes(x=as.factor(week),y=mean_epa)) +
geom_point(aes(group = seq_along(week)), color = "orange", size = 4) +
geom_line(alpha = 0.5, aes(group = 1))+
transition_reveal(week)+
geom_text(aes(label = mean_epa))
animate(p)
由 reprex package (v0.3.0)
于 2020-02-03 创建