我需要用R绘制一些3D点随时间变化的轨迹(动画)。
我尝试了这两段代码:
- 3D点随时间设置动画,但不绘制从一个时刻到下一个时刻的路径:
library(plotly)
short_all_data <- data.frame(
stringsAsFactors = FALSE,
time = c("09:06:01","09:06:01",
"09:06:01","09:06:01","09:06:01","09:06:01","09:06:01",
"09:06:01","09:06:01","09:06:01","09:06:02","09:06:02",
"09:06:02","09:06:02","09:06:02","09:06:02","09:06:02",
"09:06:02","09:06:02","09:06:02","09:06:03",
"09:06:03","09:06:03","09:06:03","09:06:03","09:06:03",
"09:06:03","09:06:03","09:06:03","09:06:03","09:06:04",
"09:06:04","09:06:04","09:06:04","09:06:04","09:06:04",
"09:06:04","09:06:04","09:06:04","09:06:04",
"09:06:05","09:06:05","09:06:05","09:06:05","09:06:05",
"09:06:05","09:06:05","09:06:05","09:06:05","09:06:05",
"09:06:06","09:06:06","09:06:06","09:06:06","09:06:06",
"09:06:06","09:06:06","09:06:06","09:06:06",
"09:06:06"),
sensor_number = c("sensor_1","sensor_2",
"sensor_3","sensor_4","sensor_5","sensor_6","sensor_7",
"sensor_8","sensor_9","sensor_10","sensor_1","sensor_2",
"sensor_3","sensor_4","sensor_5","sensor_6",
"sensor_7","sensor_8","sensor_9","sensor_10","sensor_1",
"sensor_2","sensor_3","sensor_4","sensor_5","sensor_6",
"sensor_7","sensor_8","sensor_9","sensor_10","sensor_1",
"sensor_2","sensor_3","sensor_4","sensor_5",
"sensor_6","sensor_7","sensor_8","sensor_9","sensor_10",
"sensor_1","sensor_2","sensor_3","sensor_4","sensor_5",
"sensor_6","sensor_7","sensor_8","sensor_9",
"sensor_10","sensor_1","sensor_2","sensor_3","sensor_4",
"sensor_5","sensor_6","sensor_7","sensor_8","sensor_9",
"sensor_10"),
temperature = c(12137L,12298L,10743L,10644L,
9787L,10617L,9466L,9150L,10051L,8706L,9651L,8001L,
7000L,9000L,12000L,15000L,14032L,12345L,12340L,
12342L,12300L,11000L,11100L,11200L,11300L,11400L,
11500L,11600L,11700L,11800L,12137L,12298L,10743L,
10644L,9787L,10617L,9466L,9150L,10051L,8706L,9651L,
8001L,7000L,9000L,12000L,15000L,14032L,12345L,
12340L,12342L,12300L,11000L,11100L,11200L,11300L,11400L,
11500L,11600L,11700L,11800L),
one = c(1L,5L,1L,5L,1L,5L,1L,5L,
1L,5L,2L,5L,1L,5L,2L,5L,1L,5L,2L,5L,1L,5L,
2L,5L,1L,5L,2L,5L,1L,5L,1L,5L,1L,5L,1L,5L,
1L,5L,1L,5L,2L,5L,1L,5L,2L,5L,1L,5L,2L,5L,1L,
5L,2L,5L,1L,5L,2L,5L,1L,5L),
two = c(1L,1L,5L,5L,1L,1L,5L,5L,
1L,1L,1L,2L,5L,2L,1L,2L,5L,2L,1L,2L,1L,2L,
5L,2L,1L,2L,5L,2L,1L,2L,1L,1L,5L,5L,1L,1L,
5L,5L,1L,1L,1L,2L,5L,2L,1L,2L,5L,2L,1L,2L,1L,
2L,5L,2L,1L,2L,5L,2L,1L,2L),
three = c(0L,0L,0L,0L,1L,1L,1L,1L,
2L,2L,0L,0L,2L,0L,1L,1L,2L,1L,2L,2L,2L,0L,
0L,0L,2L,1L,1L,1L,1L,2L,0L,0L,0L,0L,1L,1L,
1L,1L,2L,2L,0L,0L,2L,0L,1L,1L,2L,1L,2L,2L,2L,
0L,0L,0L,2L,1L,1L,1L,1L,2L)
)
fig <- short_all_data %>%
plot_ly(x=~one,
y=~two,
z=~three,
size = 10,
color = ~sensor_number,
frame = ~time,
mode ='lines+markers',
type = 'scatter3d')
fig
1.动画点随时间绘制从一个时刻到下一个时刻的路径,但不是3D而是2D:
library(plotly)
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
df <- txhousing
fig <- df %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area"))
fig <- fig %>% accumulate_by(~date)
fig <- fig %>%
plot_ly(
x = ~date,
y = ~median,
split = ~city,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
)
fig <- fig %>% layout(
xaxis = list(
title = "Date",
zeroline = F
),
yaxis = list(
title = "Median",
zeroline = F
)
)
fig <- fig %>% animation_opts(
frame = 100,
transition = 0,
redraw = FALSE
)
fig <- fig %>% animation_slider(
hide = T
)
fig <- fig %>% animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
fig
基本上我需要两者的结合。没有必要使用plotly,可以是其他R库。
1条答案
按热度按时间w7t8yxp51#
像这样?如果这符合你的要求,我会告诉你如何做动画。