将 ggplot/manipulate 图转换为 plotly 或 js 脚本图

Converting a ggplot/manipulate plot to a plotly or a js-scripted plot

我正在尝试使用 ggplotly() 将我的 ggplot 转换为绘图。但是,在对情节进行操作之后,它似乎不适用于此代码。还有其他方法吗?

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))

首先,为了让您的代码与 ggplot2 绘图一起工作,您的代码中存在一个问题需要修复。你不应该给你的函数和绘图对象相同的名称。替换为:

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

通过,例如:

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

现在,关于 plotly 图,我认为它不应该与操纵一起使用。我引用 RStudio 的网站 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.