在这里使用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停止执行上面的操作。
感谢您花费时间帮助...
答案 0 :(得分:2)
问题在于,将其转换为因数时,ggplot不再“知道”如何对Week变量进行分组,并且不再连接观察值。您只需要将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创建