R/plotly: Dragging Sliders Instead of Autoplay

守給你的承諾、 提交于 2021-02-10 15:16:23

问题


I am working with the R programming language. Using this previously asked question on stackoverflow ( Slider for Plotly R), I was able to make a "slider animation" (using the "plotly" library) for different values of (a variable called) "var" within the data set:

#load libraries
library(plotly)
library(dplyr)
library(ggplot2)

#generate data
var = rnorm(731, 100,25)
date= seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
data = data.frame(var,date)

#aggregate data
aggregate = data %>%
    mutate(date = as.Date(date)) %>%
    group_by(month = format(date, "%Y-%m")) %>%
    summarise( Count = n())


#analysis for "99"

data$var_99 = 99
data$new_var_99 = ifelse(data$var >99,1,0)

#percent of observations greater than 99 (each month)
aggregate_99 = data %>%
    mutate(date = as.Date(date)) %>%
    group_by(month = format(date, "%Y-%m")) %>%
    summarise( mean = mean(new_var_99))


#analysis for "98"

data$var_98 = 98
data$new_var_98 = ifelse(data$var >98,1,0)

#percent of observations greater than 98 (each month)
aggregate_98 = data %>%
    mutate(date = as.Date(date)) %>%
    group_by(month = format(date, "%Y-%m")) %>%
    summarise( mean = mean(new_var_98))

#combine files together 

aggregate_98$var = 98
aggregate_98$var = as.factor(aggregate_98$var)

aggregate_99$var = 99
aggregate_99$var = as.factor(aggregate_99$var)

combine = rbind(aggregate_98, aggregate_99)

#make animation
gg <-ggplot(combine, aes(frame = var)) + geom_line(aes(x=month, y=mean, group=1))+ theme(axis.text.x = element_text(angle=90)) + ggtitle("Average Number of Observations Each Month Greater Than ....")

ggplotly(gg)

However, it seems that it is not possible to "drag" the "circular slider button" (indicated by the red arrow). It seems I can only press the "play button" and the animation autoplays. Is there a way that this code can be modified so that you can "drag" the circular slider button across?

Thanks

EDIT: my session info

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252
[4] LC_NUMERIC=C                    LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.9.3  ggplot2_3.3.3 dplyr_1.0.3  

loaded via a namespace (and not attached):
 [1] pillar_1.4.7      compiler_4.0.3    tools_4.0.3       digest_0.6.27     jsonlite_1.7.2    lifecycle_0.2.0  
 [7] tibble_3.0.5      gtable_0.3.0      viridisLite_0.3.0 pkgconfig_2.0.3   rlang_0.4.10      cli_2.2.0        
[13] rstudioapi_0.13   withr_2.4.1       httr_1.4.2        generics_0.1.0    vctrs_0.3.6       htmlwidgets_1.5.3
[19] grid_4.0.3        tidyselect_1.1.0  glue_1.4.2        data.table_1.13.6 R6_2.5.0          fansi_0.4.2      
[25] purrr_0.3.4       tidyr_1.1.2       magrittr_2.0.1    scales_1.1.1      ellipsis_0.3.1    htmltools_0.5.1.1
[31] assertthat_0.2.1  colorspace_2.0-0  utf8_1.1.4        lazyeval_0.2.2    munsell_0.5.0     crayon_1.3.4   

回答1:


You should not have to change anything at all! Just click and drag. Your exact code works perfectly well on my end. And this isn't a new feature either since I'm on Package plotly version 4.9.1 in RStudio.

Click

Drag



来源:https://stackoverflow.com/questions/66096701/r-plotly-dragging-sliders-instead-of-autoplay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!