0
votes

I have a script using ggplotly to produce a couple of interactive charts. I then try to produce addition charts that are not interactive using ggplot. They refuse to plot even if I introduce Sys.sleep() pauses.

Is there a reason why one cannot mix the interactive Javescript ggplotly plots with non-interactive ggplot plots in the same script? I cannot find anything answers regarding this question.

Update: Here is a mini-version of the code I am using. Actually, it doesn't work in RStudio. The second plot appears to write on top of the first one. Whatever plot is created last appears to overwrite the previous plot. If I remove the call to 'ggplotly' and just print the ggplot construction, all is well. It has something to do with the call to ggplotly.Conversely, if I use ggplotly for both charts, all is well. Seems they don't mix.

library("ggplot2")
library("plotly")
test_data <- data.frame(A = c(1,5,7,4,2),
                        B = c(3,3,6,8,4),
                        C = c(6,2,9,4,5))

my_dates <- as.Date(c("2010-01-01", "2010-02-01",
                      "2010-03-01", "2010- 4-01",
                      "2010-05-01"))

xts_data <- xts(test_data, order.by = my_dates)
p <- autoplot(xts_data, facets = NULL) +
  guides(color = guide_legend(override.aes = list(size = 2))) +
  geom_line(size = 1)
print(ggplotly(p))

new_df <- data.frame(P = c(70, 70, 70),
                     Category = c("A", "B", "C"),
                     Value = c(5, 15, 10))
p <- ggplot(data = new_df, aes(
      x = Category, y = Value)) +
      geom_bar(position = position_dodge(), stat = "identity")
 print(p)
1
No idea, works for me. You might want to add more details: is it in a shiny app, or from command line, or Rstudio, what command lines do you use for plotting, a minimal reproducible example etc.January
Please read this. As I said – it works for me. The devil is in the details! You mention a script – what script? How do you execute it? What are the commands? Can you boil your script down to a few lines which you can post here?January
OK. It doesn't work when run in RStudio. I only get the second plot, which seems to plot on top of the first one.Ernie
My conclusion after running many tests is one cannot mix ggplots and plotly plots in the same piece of code. All plots just be either one type or the other. Comments or disagreements are invited.Ernie

1 Answers

0
votes

The "problem" I was having is the ggplot chart is displayed in the Plot pane and the ggplotly charts are displayed in the Viewer pane. Problem solved.