Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在帧结束后将线图绘制为GGR_R_Ggplot2_Plotly_Ggplotly - Fatal编程技术网

在帧结束后将线图绘制为GGR

在帧结束后将线图绘制为GGR,r,ggplot2,plotly,ggplotly,R,Ggplot2,Plotly,Ggplotly,我试图创造一个互动的情节。我的线条有不同的框架(有些末端更短)。播放动画时,线在其帧结束后消失。我试图用添加的帧添加更多的伪数据行,但这并没有产生任何影响。我不知道为什么会这样 样本数据: > dput(sample_n(Plot, 20)) structure(list(Strain = c(0.0114976650717703, 0.0283627375, 0.00173272727272728, 0.0150241, 0.0450557, 0.0113663, 3.97637e-0

我试图创造一个互动的情节。我的线条有不同的框架(有些末端更短)。播放动画时,线在其帧结束后消失。我试图用添加的帧添加更多的伪数据行,但这并没有产生任何影响。我不知道为什么会这样

样本数据:

> 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")
框架由以下函数创建:

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

关于上述方法,有两个问题:

  • 使用
    rbind
  • 在动画中使用
    tooltip
    参数时,似乎有一个bug(痕迹正在消失-因此我将其注释掉)
  • 请检查以下内容:

    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)
    
    library(plotly)
    图书馆(dplyr)
    
    通过积累\u上述方法有两个问题:

  • 使用
    rbind
  • 在动画中使用
    tooltip
    参数时,似乎有一个bug(痕迹正在消失-因此我将其注释掉)
  • 请检查以下内容:

    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)
    
    library(plotly)
    图书馆(dplyr)
    
    您是否试图创建一个?@ismirsehegalyes@ismirsehregal如果你看看我刚才的编辑,我已经有了
    accumulate\u by
    功能,您能为我们提供初始数据集吗?@ismirsehegal刚刚编辑以显示原始数据您是否正在尝试创建?@ismirsehegalyes@ismirsehregal如果你看看我刚才的编辑,我已经有了
    accumulate\u by
    功能,您能给我们提供初始数据集吗?@ismirsehegal刚刚编辑以显示原始数据谢谢!工具提示错误是因为我们没有框架作为输入之一。如果更改为
    tooltip=c(“应变”、“应力”、“仪器”、“杆”、“框架”),
    它运行时没有问题我认为您不希望工具提示中出现
    框架。将其添加到定义中会导致与根本不指定工具提示参数相同的行为。不过,很好,它为你工作!非常感谢。工具提示错误是因为我们没有框架作为输入之一。如果更改为
    tooltip=c(“应变”、“应力”、“仪器”、“杆”、“框架”),
    它运行时没有问题我认为您不希望工具提示中出现
    框架。将其添加到定义中会导致与根本不指定工具提示参数相同的行为。不过,很好,它为你工作!
    
    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)