r - 在框架以 r ggplotly 结束后保留折线图
问题描述
我正在尝试创建一个交互式情节。我的线条有不同的框架(有些末端较短)。当我播放动画时,线条在帧结束后消失。我尝试使用添加的帧添加更多行的假数据,但这并没有什么不同。我不确定为什么会这样。
样本数据:
> 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)
}
我有以下情节:
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)
此外,重绘似乎没有做我想要的。我希望情节以完整的图表(最后一帧和所有行)开始,然后以所有完成的内容结束。我只想要按下播放按钮时的动画。
编辑:
初始数据由多个数据帧组成。我独立地为每个数据帧累积了帧,然后将它们收集到一个带有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)
解决方案
关于您的上述方法有两个问题:
accumulate_by
使用后需要打电话rbind
- 将参数与动画一起使用时似乎存在错误
tooltip
(痕迹正在消失 - 因此我将其注释掉)
请检查以下内容:
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)
顺便说一句,使用更多数据创建累积动画的方法accumulate_by
会变慢。在这里,您可以找到另一种通过过滤数据来创建累积动画的方法。但是,R plotly 包目前不允许为动画滑块提供自定义步骤。有兴趣的请支持我的FR 。
推荐阅读
- jquery - 添加自定义按钮后,如何更新购物车总数?
- php - PHP表单循环仅提交最后一个值或所有值
- gnuplot - 如何在 gnuplot 的 xlabel 中添加乘号?
- eslint - ESLint 导入/排序规则在不同机器上的工作方式不同 - 错误 `..` 导入应该在导入 `.` 之前发生
- asp.net-core - 如何在带有 Servicestack 的 .NET Core 中使用 Windows 身份验证登录
- reactjs - 反应:反应路由器链接未加载组件
- javascript - TS 错误,“类型‘查询’不可分配给类型‘字符串’”
- c++ - 在 C++ 中通过参数创建不同类型的对象?
- can-bus - 使用 CAPL 播放 Vector CANoe BLF/ASC 文件
- bash - 使用正则表达式 (*) 对特定文件进行 scp