0

I am using the R programming language. Using the following code, I made these interactive time series graphs:

library(dplyr)
library(ggplot2)
library(shiny)
library(plotly)
library(htmltools)

library(dplyr)
#generate data
set.seed(123)

######

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

vals <- 90:100
combine <- vector('list', length(vals))
count <- 0
for (i in vals) {
    
    data$var_i = i
    data$new_var_i = ifelse(data$var >i,1,0)
    
    #percent of observations greater than i (each month)
    aggregate_i = data %>%
        mutate(date = as.Date(date)) %>%
        group_by(month = format(date, "%Y-%m")) %>%
        summarise( mean = mean(new_var_i))
    
    #combine files together
    
    aggregate_i$var = i
    aggregate_i$var = as.factor(aggregate_i$var)
    
    count <- count + 1
    combine[[count]] <- aggregate_i
    
}


result_2 <- bind_rows(combine)
result_2$group = "group_b"
result_2$group = as.factor(result_2$group)

a = result_2 %>%
  plot_ly(x=~month, y=~mean, color=~group) %>%
  group_by(group) %>%
  add_lines(frame=~var,hoverinfo = "text",
            text = ~ paste0("Month: ",month, "<br>",
                            "Mean: ", mean, "<br>",
                            "Total: ", mean(mean))) %>%
  layout(title = list(text = "title"),
         xaxis = list(tickangle = -90, tickformat = "%m-%Y"))



######


var = rnorm(731, 75,2)
date= seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
data = data.frame(var,date)

vals <- 75:80
combine <- vector('list', length(vals))
count <- 0
for (i in vals) {
    
    data$var_i = i
    data$new_var_i = ifelse(data$var >i,1,0)
    
    #percent of observations greater than i (each month)
    aggregate_i = data %>%
        mutate(date = as.Date(date)) %>%
        group_by(month = format(date, "%Y-%m")) %>%
        summarise( mean = mean(new_var_i))
    
    #combine files together
    
    aggregate_i$var = i
    aggregate_i$var = as.factor(aggregate_i$var)
    
    count <- count + 1
    combine[[count]] <- aggregate_i
    
}


result_3 <- bind_rows(combine)
result_3$group = "group_b"
result_3$group = as.factor(result_3$group)

b = result_3 %>%
  plot_ly(x=~month, y=~mean, color=~group) %>%
  group_by(group) %>%
  add_lines(frame=~var,hoverinfo = "text",
            text = ~ paste0("Month: ",month, "<br>",
                            "Mean: ", mean, "<br>",
                            "Total: ", mean(mean))) %>%
  layout(title = list(text = "title"),
         xaxis = list(tickangle = -90, tickformat = "%m-%Y"))

##### https://stackoverflow.com/questions/66180563/r-updating-hover-text


 a = plot_ly(result_2, x=~month, y=~mean, color=~group) %>%
  group_by(group) %>%
  add_lines(frame=~var,hoverinfo = "text",
            text = ~ paste0("Month: ",month, "<br>",
                            "Mean: ", mean, "<br>",
                            "Total: ", mean(mean))) %>%
  layout(title = list(text = "title1"),
         xaxis = list(tickangle = -90, tickformat = "%m-%Y"))


b = plot_ly(result_3, x=~month, y=~mean, color=~group) %>%
  group_by(group) %>%
  add_lines(frame=~var,hoverinfo = "text",
            text = ~ paste0("Month: ",month, "<br>",
                            "Mean: ", mean, "<br>",
                            "Total: ", mean(mean))) %>%
  layout(title = list(text = "title2"),
         xaxis = list(tickangle = -90, tickformat = "%m-%Y"))

#view plots

a

b

These two graphs work fine when you view them individually:

enter image description here

But when I try to use the plotly::subplot() command, the second graph disappears:

subplot(a,b, nrows=1)

enter image description here

Does anyone know why this is happening and is there a way to fix this? Is there also a way to use the ggplot2::facet_wrap() command in this instance to "synchronize" both graphs together (like here: R: "tie" two graphs together) ?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

2

Your frame axes dont intersect at any point so you wont expect to see both charts at the same time Try this and press play and you will see the transition.


subplot(b,a) %>%
  animation_opts(frame = 300, transition = 100, easing="elastic") %>%
  animation_slider(currentvalue = list(prefix=NULL, font=list(color="red", size=40)))

Each chart will appear when the frame number gets into its frame range.

zimia
  • 930
  • 3
  • 16
  • thank you for your answer! this again worked perfectly! is it possible to have two separate "sliders" - one for each graph? – stats_noob Feb 13 '21 at 02:31
  • 1
    I am afraid this is something I am not aware of. But if there is, you will probably find it here https://community.plotly.com/c/api/r/9 after some digging around – zimia Feb 13 '21 at 02:44
  • Thanks... I'll keep digging. Your help has been greatly Appreciated. Did you have any ideas for this one here!: https://stackoverflow.com/questions/66161597/gluing-graphs-together-trouble-with-traces-operator-is-invalid-for-atomic ... i am thinking of putting a bounty on it tomorrow – stats_noob Feb 13 '21 at 02:48