J'essaye de créer une intrigue interactive. Mes lignes ont des cadres différents (certains se terminent plus courts). Lorsque je joue l'animation, les lignes disparaissent après la fin de leur image. J'ai essayé d'ajouter plus de lignes de fausses données avec les cadres ajoutés, mais cela ne faisait aucune différence. Je ne sais pas pourquoi cela se produit.

Exemples de données:

> dput(sample_n(Plot, 20))
structure(list(Strain = c(0.0114976650717703, 0.0283627375, 0.00173272727272728, 
0.0150241, 0.0450557, 0.0113663, 3.97637e-05, 0.0434292, 0.00406928, 
0.00207895693779905, 0.000215139, 0.00148572499999999, 0.0418233875, 
0.000939926, 0.000255219, 0.0213334, 0.023816, 0.0145059, 0.0131467081339713, 
0.0668966), Stress = c(75.9505644091148, 78.5302055162329, 48.6062116858257, 
68.4024611345473, 85.2904610049016, 63.9140059100703, 1.43583171490823, 
84.8256506176318, 64.1272033757161, 53.8795512181689, 14.442082799196, 
64.3587637619208, 84.731707366194, 25.4200560130662, 13.8571035440045, 
84.1313452985436, 85.4161843153761, 67.9158653588222, 77.7143709896995, 
90.4239965485986), Instrumentation = structure(c(2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 
1L), .Label = c("DIC", "LVDT"), class = "factor"), Bar = structure(c(1L, 
2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
2L, 1L, 2L), .Label = c("Spliced", "Unspliced"), class = "factor"), 
ANIM = c(0.01, 0.03, 0, 0.02, 0.05, 0.01, 0, 0.04, 0, 0, 
0, 0, 0.04, 0, 0, 0.02, 0.02, 0.01, 0.01, 0.07), frame = c(0.05, 
0.05, 0.04, 0.13, 0.05, 0.1, 0.01, 0.11, 0.02, 0, 0.04, 0.11, 
0.09, 0.01, 0.01, 0.04, 0.06, 0.09, 0.05, 0.08)), row.names = c(NA, 
-20L), class = "data.frame")

Les cadres sont créés par la fonction:

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

J'ai ce qui suit pour l'intrigue:

plot <- ggplot(Plot) + aes(x = Strain, y = Stress, colour = Instrumentation, linetype = Bar, frame = frame) +
 scale_color_manual(values = c("DIC"="red", "LVDT"="blue")) + geom_line(size = 0.8) + theme_classic() + 
 labs(x = "Strain (in/in)", y = "Stress (ksi)") + 
 theme_classic() + theme(axis.text =  element_text(color = "black", size = 16), 
   axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
   axis.title.y = element_text(color = "black", size = 20, margin = margin(0,40,0,0)), 
   axis.title.x = element_text(color = "black", size = 20, margin = margin(35,0,0,0)),
   legend.title = element_blank(), legend.text = element_text(color = "black", size = 16))


ggplotly(
  p = ggplot2::last_plot(),
  width = NULL,
  height = NULL,
  tooltip = c("Strain","Stress","Instrumentation","Bar"),
  dynamicTicks = FALSE,
  layerData = 1,
  originalData = TRUE,) %>% animation_opts(frame = 300, transition = 0, easing = "linear", redraw = TRUE) %>%
  layout(yaxis = list(title = list(text = "Stress (ksi)", standoff = 30L), spikesides = TRUE,spikethickness = 1),
         xaxis = list(title = list(text = "Strain (in/in)",standoff = 30L), spikesides = TRUE,spikethickness = 1),
         legend = list(orientation = "v", x = 0.7, y = 0.13)) %>% animation_slider(hide = TRUE)

De plus, le redessiner ne semble pas faire ce que je veux. Je veux que l'intrigue commence par le graphique complet (dernière image et toutes les lignes), puis se termine par tout terminé. Je veux seulement l'animation en appuyant sur le bouton de lecture.

MODIFIÉ:

Les données initiales sont constituées de plusieurs dataframes. J'ai accumulé les frames pour chaque dataframe indépendamment, puis les ai collectées dans une seule dataframe avec rbind.

> dput(sample_n(Plot.Spliced_DIC, 5))
structure(list(Strain = c(0.00635239, 0.00646411, 0.00304078, 
0.00104394, 0.00138191), Stress = c(64.9199586945409, 65.0674673934705, 
60.7649363239015, 26.5627079525764, 39.4159208404832), Instrumentation = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "DIC", class = "factor"), Bar = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "Spliced", class = "factor"), ANIM = c(0.01, 
0.01, 0, 0, 0)), class = "data.frame", row.names = c(NA, 
-5L))

> dput(sample_n(Plot.Unspliced_DIC, 5))
structure(list(Strain = c(7.66444e-05, 0.112481, 6.7294e-05, 
0.00275991, 0.0600871), Stress = c(0.00344797841982851, 92.8897366602444, 
0.522824377824084, 64.3513766859271, 88.2700702030104), Instrumentation = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "DIC", class = "factor"), Bar = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "Unspliced", class = "factor"), ANIM = c(0, 
0.11, 0, 0, 0.06)), class = "data.frame", row.names = c(NA, 
-5L))

> dput(sample_n(Plot.Unspliced_LVDT, 5))
structure(list(Strain = c(0.071858225, 0.0020442, 0.01736605, 
0.0259565, 0.025441275), Stress = c(90.4766041042011, 60.701740404307, 
70.0774864366534, 76.21702132901, 75.9110679987475), Instrumentation = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "LVDT", class = "factor"), Bar = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "Unspliced", class = "factor"), ANIM = c(0.07, 
0, 0.02, 0.03, 0.03)), class = "data.frame", row.names = c(NA, 
-5L))

> dput(sample_n(Plot.Spliced_LVDT, 5))
structure(list(Strain = c(0.0238954832535885, 0.00040463157894736, 
0.0158640956937799, 0.0224346507177033, 0.00269568421052631), 
Stress = c(85.3207044456991, 14.881393139828, 79.7864191978379, 
84.4980682215488, 56.6941352672259), Instrumentation = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "LVDT", class = "factor"), Bar = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = "Spliced", class = "factor"), ANIM = c(0.02, 
0, 0.02, 0.02, 0)), class = "data.frame", row.names = c(NA, -5L))

Plot.Spliced_DIC <- Plot.Spliced_DIC %>% accumulate_by(~ANIM)
Plot.Unspliced_DIC <- Plot.Unspliced_DIC %>% accumulate_by(~ANIM)
Plot.Unspliced_LVDT <- Plot.Unspliced_LVDT %>% accumulate_by(~ANIM)
Plot.Spliced_LVDT <- Plot.Spliced_LVDT %>% accumulate_by(~ANIM)

Plot <- rbind(Plot.Spliced_DIC, Plot.Unspliced_DIC, Plot.Unspliced_LVDT, Plot.Spliced_LVDT)
0
Maral Dorri 25 févr. 2021 à 03:31

1 réponse

Meilleure réponse

Il y a deux problèmes concernant votre approche ci-dessus:

  1. vous devez appeler accumulate_by après avoir utilisé rbind
  2. Il semble y avoir un bug lors de l'utilisation de l'argument tooltip avec une animation (les traces disparaissent - je l'ai donc commenté)

S'il vous plaît vérifier les points suivants:

library(plotly)
library(dplyr)

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

Plot.Spliced_DIC <-
structure(list(Strain = c(0.00635239, 0.00646411, 0.00304078, 0.00104394, 0.00138191),
               Stress = c(64.9199586945409, 65.0674673934705, 60.7649363239015, 26.5627079525764, 39.4159208404832),
               Instrumentation = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "DIC", class = "factor"),
               Bar = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Spliced", class = "factor"),
               ANIM = c(0.01, 0.01, 0, 0, 0)), class = "data.frame", row.names = c(NA, -5L))

Plot.Unspliced_DIC <-
structure(list(Strain = c(7.66444e-05, 0.112481, 6.7294e-05, 0.00275991, 0.0600871),
               Stress = c(0.00344797841982851, 92.8897366602444, 0.522824377824084, 64.3513766859271, 88.2700702030104),
               Instrumentation = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "DIC", class = "factor"),
               Bar = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Unspliced", class = "factor"),
               ANIM = c(0, 0.11, 0, 0, 0.06)), class = "data.frame", row.names = c(NA, -5L))

Plot.Unspliced_LVDT <-
structure(list(Strain = c(0.071858225, 0.0020442, 0.01736605,0.0259565, 0.025441275),
               Stress = c(90.4766041042011, 60.701740404307, 70.0774864366534, 76.21702132901, 75.9110679987475),
               Instrumentation = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "LVDT", class = "factor"),
               Bar = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Unspliced", class = "factor"),
               ANIM = c(0.07,0, 0.02, 0.03, 0.03)), class = "data.frame", row.names = c(NA, -5L))

Plot.Spliced_LVDT <-
  structure(list(Strain = c(0.0238954832535885, 0.00040463157894736, 
                            0.0158640956937799, 0.0224346507177033, 0.00269568421052631), 
                 Stress = c(85.3207044456991, 14.881393139828, 79.7864191978379, 
                            84.4980682215488, 56.6941352672259), 
                 Instrumentation = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "LVDT", class = "factor"),
                 Bar = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Spliced", class = "factor"), 
                 ANIM = c(0.02, 0, 0.02, 0.02, 0)), class = "data.frame", row.names = c(NA, -5L))

plotData <- rbind(Plot.Spliced_DIC, Plot.Unspliced_DIC, Plot.Unspliced_LVDT, Plot.Spliced_LVDT)
plotData <- plotData %>% accumulate_by(~ANIM)

lastFrame <- with(plotData, plotData[frame == max(frame),])
lastFrame$frame <- -1

plotData <- rbind(lastFrame, plotData) # prepend last frame to show all complete traces in the beginning

plot <- ggplot(plotData) + aes(x = Strain, y = Stress, colour = Instrumentation, linetype = Bar, frame = frame) +
  scale_color_manual(values = c("DIC"="red", "LVDT"="blue")) + geom_line(size = 0.8) + theme_classic() + 
  labs(x = "Strain (in/in)", y = "Stress (ksi)") + 
  theme_classic() + theme(axis.text =  element_text(color = "black", size = 16), 
                          axis.line = element_line(color = "black", size = 0.2), axis.ticks.y = element_line(color = "black", size = 0.2),
                          axis.title.y = element_text(color = "black", size = 20, margin = margin(0,40,0,0)), 
                          axis.title.x = element_text(color = "black", size = 20, margin = margin(35,0,0,0)),
                          legend.title = element_blank(), legend.text = element_text(color = "black", size = 16))

ggplotly(
  p = ggplot2::last_plot(),
  width = NULL,
  height = NULL,
  # tooltip = c("Strain","Stress","Instrumentation","Bar"), # bug?
  # tooltip = c("x","y","colour","linetype"), # bug?
  dynamicTicks = FALSE,
  layerData = 1,
  originalData = TRUE) %>%
  animation_opts(frame = 300, transition = 0, easing = "linear", redraw = TRUE) %>%
  layout(yaxis = list(title = list(text = "Stress (ksi)", standoff = 30L), spikesides = TRUE,spikethickness = 1),
         xaxis = list(title = list(text = "Strain (in/in)",standoff = 30L), spikesides = TRUE,spikethickness = 1),
         legend = list(orientation = "v", x = 0.7, y = 0.13)) %>% 
  animation_slider(hide = TRUE)

result

À propos, l'approche pour créer des animations cumulatives à l'aide de accumulate_by sera tourner plus lentement avec plus de données. Ici, vous pouvez trouver une autre approche pour créer une animation cumulative en filtrant les Les données. Cependant, le package R plotly ne permet actuellement pas de fournir des étapes personnalisées pour les curseurs d'animation. Veuillez soutenir mon FR si vous êtes intéressé.

2
ismirsehregal 3 mars 2021 à 09:10