首页 > 解决方案 > 将 ggplot/manipulate 图转换为 plotly 或 js 脚本图

问题描述

我正在尝试使用 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))

标签: rggplot2r-plotly

解决方案


首先,为了使您的代码与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 与操作包一起为标准R 绘图添加交互功能。


推荐阅读