我正在尝试制作一个动画情节,展示 NBA 球队的三分出手率和助攻率如何随时间变化。虽然图中的点正确过渡,但我尝试添加垂直和水平平均线,但是总体平均值保持不变,而不是逐年变化。
p<-ggplot(dataBREFPerPossTeams, aes(astPerPossTeam,fg3aPerPossTeam,col=ptsPerPossTeam))+
geom_point()+
scale_color_gradient(low='yellow',high='red')+
theme_classic()+
xlab("Assists Per 100 Possessions")+
ylab("Threes Attempted Per 100 Possessions")+labs(color="Points Per 100 Possessions")+
geom_hline(aes(yintercept = mean(fg3aPerPossTeam)), color='blue',linetype='dashed')+
geom_vline(aes(xintercept = mean(astPerPossTeam)), color='blue',linetype='dashed')
anim<-p+transition_time(as.integer(yearSeason))+labs(title='Year: {frame_time}')
animate(anim, nframes=300)
理想情况下,两条虚线会随着时间的推移而变化,但目前它们保持不变。有想法该怎么解决这个吗?
我在用datasets::airquality
因为您还没有共享您的数据。这里的想法是,您需要拥有其他几何图形的值(这里是mean
)作为数据集中的变量,所以gganimate
可以绘制值之间的联系frame
(i.e. transition_time
).
所以我所做的是分组frame
(这里是月份,它将是yearSeason
为你),然后用我想要的变量的平均值来改变一列。然后在geoms
我使用了附加变量而不是获取mean
里面的geom
。往下看;
library(datasets) #datasets::airquality
library(ggplot2)
library(gganimate)
library(dplyr)
g <- airquality %>%
group_by(Month) %>%
mutate(mean_wind=mean(Wind),
mean_temp=mean(Temp)) %>%
ggplot()+
geom_point(aes(Wind,Temp, col= Solar.R))+
geom_hline(aes(yintercept = mean_temp), color='blue',linetype='dashed')+
geom_vline(aes(xintercept = mean_wind), color='green',linetype='dashed')+
scale_color_gradient(low='yellow',high='red')+
theme_classic()+
xlab("Wind")+
ylab("Temp")+labs(color="Solar.R")
animated_g <- g + transition_time(as.integer(Month))+labs(title='Month: {frame_time}')
animate(animated_g, nframes=18)
Created on 2019-06-09 by the reprex package https://reprex.tidyverse.org (v0.3.0)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)