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

06odsfpq  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(131)

我需要用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.动画点随时间绘制从一个时刻到下一个时刻的路径,但不是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库。

w7t8yxp5

w7t8yxp51#

像这样?如果这符合你的要求,我会告诉你如何做动画。

pts <- cbind(short_all_data$one, short_all_data$two, short_all_data$three)

library(rgl)
open3d(windowRect = 50 + c(0, 0, 512, 512))
view3d(45, 45)
# bounding sphere (invisible)
spheres3d(x = 2.5, y = 2.5, z = 2.5, radius = 2.5, alpha = 0)
bbox3d(color = c("#333377", "black"), emission = "#333377", 
       specular = "#3333FF", shininess = 5, alpha = 0.8)
for(i in 1:(nrow(pts)-1)){
  spheres3d(pts[i, ], radius = 0.1)
  lines3d(pts[c(i,i+1), ])
  Sys.sleep(0.5)
}

相关问题