Plotly 在 R 中创建 3D 动画

Plotly in R creating a 3D animation

我一直在尝试在 Plotly 中制作动画 3D 散点图,以显示传感器数据如何随时间变化。 Ploty 似乎正确地对时间范围进行了分类,并将所有传感器信息放在一个时间范围内,但是它并没有在每个时间范围内更新轨迹,而是将另一个时间范围添加到所有时间范围内。关于如何在每个时间范围内重新绘制的任何想法?我的密码是

fig1 <- short_all_data %>%
  plot_ly(x=~one, 
          y=~two, 
          z=~three,
          size = ~temperature,
          color = ~temperature,
          frame = ~time,
          mode ='markers',
          type = 'scatter3d')
  fig1 

数据框是

structure(list(time = structure(c(32761, 32761, 32761, 32761, 
32761, 32761, 32761, 32761, 32761, 32761, 32761, 32761, 32761, 
32761, 32761, 32761, 32761, 32761, 32761, 32761, 32761, 32761, 
32761, 32761, 32775, 32775, 32775, 32775, 32775, 32775, 32775, 
32775, 32775, 32775, 32775, 32775, 32775, 32775, 32775, 32775, 
32775, 32775, 32775, 32775, 32775, 32775, 32775, 32775), class = c("hms", 
"difftime"), units = "secs"), sensor_number = c("sensor_1", "sensor_2", 
"sensor_3", "sensor_4", "sensor_5", "sensor_6", "sensor_7", "sensor_8", 
"sensor_9", "sensor_10", "sensor_11", "sensor_12", "sensor_13", 
"sensor_14", "sensor_15", "sensor_16", "sensor_17", "sensor_18", 
"sensor_19", "sensor_20", "sensor_21", "sensor_22", "sensor_23", 
"sensor_24", "sensor_1", "sensor_2", "sensor_3", "sensor_4", 
"sensor_5", "sensor_6", "sensor_7", "sensor_8", "sensor_9", "sensor_10", 
"sensor_11", "sensor_12", "sensor_13", "sensor_14", "sensor_15", 
"sensor_16", "sensor_17", "sensor_18", "sensor_19", "sensor_20", 
"sensor_21", "sensor_22", "sensor_23", "sensor_24"), temperature = c(12137, 
12298, 10743, 10644, 9787, 10617, 9466, 9150, 10051, 8706, 10094, 
9541, 10326, 9983, 9494, 10044, 10126, 10436, 11848, 12141, 12216, 
11348, 0, 0, 12422, 11876, 10556, 10763, 10284, 10365, 8760, 
9684, 10068, 9649, 9940, 9913, 10316, 9527, 9939, 10378, 9920, 
9986, 11946, 12461, 11804, 11230, 50000, 0), one = c(1, 5, 1, 
5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 
1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 5, 1, 
5, 1, 5), two = c(1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 5, 
5, 1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 5, 5, 
1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 5, 5), three = c(0, 0, 0, 0, 1, 
1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 
0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 
5)), row.names = c(NA, -48L), groups = structure(list(sensor_number = c("sensor_1", 
"sensor_10", "sensor_11", "sensor_12", "sensor_13", "sensor_14", 
"sensor_15", "sensor_16", "sensor_17", "sensor_18", "sensor_19", 
"sensor_2", "sensor_20", "sensor_21", "sensor_22", "sensor_23", 
"sensor_24", "sensor_3", "sensor_4", "sensor_5", "sensor_6", 
"sensor_7", "sensor_8", "sensor_9"), .rows = structure(list(c(1L, 
25L), c(10L, 34L), c(11L, 35L), c(12L, 36L), c(13L, 37L), c(14L, 
38L), c(15L, 39L), c(16L, 40L), c(17L, 41L), c(18L, 42L), c(19L, 
43L), c(2L, 26L), c(20L, 44L), c(21L, 45L), c(22L, 46L), c(23L, 
47L), c(24L, 48L), c(3L, 27L), c(4L, 28L), c(5L, 29L), c(6L, 
30L), c(7L, 31L), c(8L, 32L), c(9L, 33L)), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 24L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

produced plot

如有任何帮助,我们将不胜感激

我稍微扩展了您的示例数据。您的代码给出了我期望的输出:

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)
)

fig1 <- short_all_data %>%
  plot_ly(x=~one, 
          y=~two, 
          z=~three,
          size = ~temperature,
          color = ~temperature,
          frame = ~time,
          mode ='markers',
          type = 'scatter3d')
fig1