2017-04-03 54 views
0

我在plot_ly在R.圖形與滑塊過濾其數據繪製

簡單數據幀(c_X,c_Y,c_Z,時間)

欲第一圖表時的時間數據幀等於零。

然後,我想要一個滑塊從0到1,當滑塊值爲1時,過濾器會顯示只有time = 1的數據的x,y,z散點圖,並且當滑塊值爲0 ,僅在列時間= 0時過濾數據。

test_x = rnorm(50) 
test_y = rnorm(50) 
test_z = rnorm(50) 
time_0 = rep(0, 50) 
df_list_1 = list('c_X' = test_x, 'c_Y' = test_y, 'c_Z' = test_z, 'time' = time_0) 
df_1 = as.data.frame(df_list_1) 

test_x2 = rnorm(50) 
test_y2 = rnorm(50) 
test_z2 = rnorm(50) 
time_1 = rep(1, 50) 
df_list_2 = list('c_X' = test_x2, 'c_Y' = test_y2, 'c_Z' = test_z2, 'time' = time_1) 
df_2 = as.data.frame(df_list_2) 

df_test = rbind(df_1, df_2) 
p_all <- plot_ly(df_test, x = ~c_X, y = ~c_Y, z = ~c_Z) 
p_all 

回答

1

您可以爲每條曲線visible屬性設置爲TRUEFALSE。在Plotly的網站https://plot.ly/r/sliders/#sine-wave-slider上有一個例子。你的問題的粗略實施將會是這樣的:

test_x = rnorm(50) 
test_y = rnorm(50) 
test_z = rnorm(50) 
time_0 = rep(0, 50) 
df_list_1 = list('c_X' = test_x, 'c_Y' = test_y, 'c_Z' = test_z, 'time' = time_0) 
df_1 = as.data.frame(df_list_1) 

test_x2 = rnorm(50) 
test_y2 = rnorm(50) 
test_z2 = rnorm(50) 
time_1 = rep(1, 50) 
df_list_2 = list('c_X' = test_x2, 'c_Y' = test_y2, 'c_Z' = test_z2, 'time' = time_1) 
df_2 = as.data.frame(df_list_2) 


steps <- list(
    list(args = list("visible", c(TRUE,TRUE)), 
     label = "Time 1 + 2", 
     method = "restyle", 
     value = "1 + 2" 
), 
    list(args = list("visible", c(FALSE,TRUE)), 
     label = "Time 0", 
     method = "restyle", 
     value = "1" 
), 
    list(args = list("visible", c(TRUE,FALSE)), 
     label = "Time 1", 
     method = "restyle", 
     value = "2" 
) 
) 

p_all <- plot_ly(type = 'scatter3d', mode = 'markers') %>% 
    add_trace(x = df_1$c_X, y = df_1$c_Y, z = df_1$c_Z, name='Time 0') %>% 
    add_trace(x = df_2$c_X, y = df_2$c_Y, z = df_2$c_Z, name='Time 1') %>% 
    layout(title = "Visible?", 
     sliders = list(
      list(
      active = 0, 
      currentvalue = list(prefix = "Time: "), 
      pad = list(t = 60), 
      steps = steps))) 
p_all