如何在 R 中绘制 3D 点随时间的轨迹路径

问题描述 投票:0回答:0

我需要用 R 绘制一些 3D 点随时间变化的轨迹(动画)。

我试过这两段代码:

  1. 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. 随着时间的推移动画点绘制从一个时刻到下一个时刻的路径,但 2D 而不是 3D:
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库即可。

r animation plot 3d trace
© www.soinside.com 2019 - 2024. All rights reserved.