0

I'm trying to convert my ggplot to a plotly plot using ggplotly(). However, it doesn't seem to work on this code, after manipulate is acted on the plot. Is there any other way to do it?

library(ggplot2)
library(manipulate)
grades <- data.frame(Final = 20 * runif(70))
myFinalsPlot <- function(sliderInput, initialIndex, finalIndex) {
  ggplot(data.frame(grades$Final[initialIndex:finalIndex]),
     aes(x = grades$Final[initialIndex:finalIndex])) +
geom_histogram(aes(y = ..density..),
               binwidth = sliderInput, colour = "green", fill = "yellow") +
geom_density(alpha = 0.2, fill = "#FF6666") +
labs(x = "Marks", y = "Grades")
}

myFinalsPlot <- manipulate(myFinalsPlot(slidersInput, 1, 70),
                       slidersInput = slider(1, 12, step = 1, initial = 5))
byouness
  • 1,746
  • 2
  • 24
  • 41
  • This looks to be an Rmarkdown doc with Shiny, right? For us to be able to help you, could you include more info (maybe a simplified version of your Rmarkdown doc with a sample data) ? Also, if you call ggplotly() on your doc in the console (no Shiny involved), does it work? – byouness May 07 '18 at 11:37
  • Noted. Could you indicate where the `manipulate()` function lies, i.e. which package? – byouness May 07 '18 at 14:32
  • No actually, this a standalone R file. I've just generated random data via rnorm, computed totals and allocated grades. I wanted to visualize it online, using a js tool. Directly calling ggplotly() doesn't work; it gives the following error: Error in UseMethod("ggplotly", p) : no applicable method for 'ggplotly' applied to an object of class "NULL" NULL is what is returned by manipulated, when checked by class(myFinalsPlot) I seem lost here. – Khurram Waqas Malik May 07 '18 at 14:33
  • manipulate() is in manipulate package – Khurram Waqas Malik May 07 '18 at 14:34
  • Posted an answer. I don't think it is supposed to work with plotly plots. For ggplot though, it worked for me after fixing an issue in your code :) – byouness May 07 '18 at 14:45
  • I added the packages to your code sample and also included a sample data (using runif). – byouness May 07 '18 at 14:52

1 Answers1

0

First, to make your code work with the ggplot2 plot, there is an issue in your code that you need to fix. You shouldn't give the same name to your function and plot object. Replace this:

myFinalsPlot <- manipulate(myFinalsPlot(slidersInput, 1, 70),
                   slidersInput = slider(1, 12, step = 1, initial = 5))

By, e.g.:

myPlot <- manipulate(myFinalsPlot(slidersInput, 1, 70),
                   slidersInput = slider(1, 12, step = 1, initial = 5))  

Now, regarding plotly plots, I don't think it is supposed to work with manipulate. I quote RStudio's website https://support.rstudio.com/hc/en-us/articles/200551906-Interactive-Plotting-with-Manipulate:

RStudio works with the manipulate package to add interactive capabilities to standard R plots.

byouness
  • 1,746
  • 2
  • 24
  • 41
  • Thanks for the assistance. I've corrected my code as such. Just on an aside, can you guide if I want to show count for histogram on primary y axis and density values on secondary y axis. – Khurram Waqas Malik May 07 '18 at 18:25
  • There are countless discussions are the topic of having a secondary axis in ggplot, see for example this answer: https://stackoverflow.com/a/3101876/2699660 and the other comments/answers in the thread. – byouness May 07 '18 at 21:52