我有一个data.frame,其中包含带有时间戳的各种事件,并进行了地理位置定位。我知道如何使用key = ActiveSupport::KeyGenerator
.new(Devise.secret_key)
.generate_key("put some random or the name of the key")
loop do
raw = SecureRandom.urlsafe_base64(nil, false)
enc = OpenSSL::HMAC.hexdigest('SHA256', key, raw)
break [raw, enc] unless Model.exist?(token: enc)
end
(*)逐小时绘制每个事件的动画。就像这样:
gganimate
现在,我想在屏幕上延长点的“停留”时间,例如,如果某个事件是在5:00 pm,我希望它从2pm到8pm(在他的位置前后3帧,并尽可能淡入和淡出)。我不知道如何使用gganimate来做到这一点,我尝试使用df = data.frame("id" = runif(500, 1e6, 1e7),
'lat' = runif(500, 45, 45.1),
'long'= runif(500, 45, 45.1),
'datetime'= seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=500),
'hour'=format(seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=500), "%H"),
'event'=paste0("type", rpois(500, 1)))
ggplot(data=df) +
aes(x=long, y=lat, color=factor(event)) +
geom_point() +
transition_states(hour, state_length = 1, transition_length = 0)
,但这使要点“移动”了,这对我来说毫无意义!
谢谢
(*)编辑:我想为每行添加6个重复的行,并将小时数从-1修改为+3,但是它要重得多,无法处理淡入/出
transition_length
答案 0 :(得分:1)
您可以使用transition_components
并指定3小时作为每个点的输入/退出时间。
数据:
set.seed(123)
n <- 50 # 500 in question
df = data.frame(
id = runif(n, 1e6, 1e7),
lat = runif(n, 45, 45.1),
long = runif(n, 45, 45.1),
datetime = seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=n),
hour = format(seq.POSIXt(from=Sys.time(), to=Sys.time()+1e5, length.out=n), "%H"),
event = paste0("type", rpois(n, 1)))
代码:
df %>%
mutate(hour = as.numeric(as.character(hour))) %>%
ggplot() +
aes(x=long, y=lat, group = id, color=factor(event)) +
# I'm using geom_label to indicate the time associated with each
# point & highlight the transition times before / afterwards.
# replace with geom_point() as needed
geom_label(aes(label = as.character(hour))) +
# geom_point() +
transition_components(hour,
enter_length = 3,
exit_length = 3) +
enter_fade() +
exit_fade() +
ggtitle("Time: {round(frame_time)}")
此方法也适用于datetime变量:
df %>%
ggplot() +
aes(x = long, y = lat, group = id, color = factor(event)) +
geom_label(aes(label = format(datetime, "%H:%M"))) +
transition_components(datetime,
enter_length = as_datetime(hm("3:0")),
exit_length = as_datetime(hm("3:0"))) +
enter_fade() +
exit_fade() +
ggtitle("Time: {format(frame_time, '%H:%M')}")
答案 1 :(得分:0)
gganimate
似乎没有设置为处理绘图上的离开点。我认为您必须走手动路线。
这是一种(略显笨拙)的方法来复制行,包括设置行应显示的时间和偏移量(用于alpha
来控制淡入淡出):
df_withRange <-
df %>%
mutate(hour = parse_number(hour)) %>%
split(1:nrow(.)) %>%
lapply(function(x){
lapply(-3:3, function(this_time){
x %>%
mutate(frame_time = hour + this_time
, offset = this_time
, abs_offset = abs(this_time))
}) %>%
bind_rows()
}) %>%
bind_rows() %>%
mutate(
frame_time = ifelse(frame_time > 23, frame_time - 24, frame_time)
, frame_time = ifelse(frame_time < 0, frame_time + 24, frame_time)
)
然后,此代码设置情节:
ggplot(data=df_withRange
, aes(x=long
, y=lat
, color=factor(event)
, alpha = abs_offset
)) +
geom_point() +
transition_states(frame_time) +
labs(title = 'Hour: {closest_state}') +
scale_alpha_continuous(range = c(1,0.2))
剧情:
还有很多工作要做(例如,淡入淡出等),但这至少应该是一个开始