Ernie
Ernie

Reputation: 1147

Using ggplotly with ggplot

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)

Upvotes: 0

Views: 506

Answers (1)

Ernie
Ernie

Reputation: 1147

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.

Upvotes: 0

Related Questions