我的示例数据集:
library(plotly)
library(htmlwidgets)
d <-
data.frame(
"name" = c("bily", "mary", "ken"),
"fruit" = c("cherry", "apple", "apple"),
"count" = c(1, 10, 30)
)
gg <- ggplot(data = d,
aes(x = fruit,
y = count,
group = name)) +
geom_line(aes(color = name)) +
geom_point(aes(color = name))
plotly <- ggplotly(gg)
保存图表:
d <- # please put your directory where you want to save the graph
"xxxx/graph.html"
我搜索了这三个可以输出HTML格式的函数,但它们也会生成其他文件,我无法解决。
#htmlwidgets::saveWidget(as_widget(plotly), d)
#htmltools::browsable(plotly)
htmltools::save_html(plotly, d)
我正在尝试将我的图表(使用 ggplot 函数到 ggplotly 函数)转换为 HTML 格式,但我遇到了问题。当我保存图表时,该函数还会生成其他文件,例如plotlyjs、jquery和crosstalk。我希望只能制作HTML文件,因为如果每个图都有不同的文件,那么将文件放在网站上太困难了。
这些是我运行代码时的文件。我的预期结果是只制作了 HTML 文件。
其他文件保存在lib目录中,但我希望它们没有提供,并且我可以运行HTML文件。可以做吗?或者还有其他方法吗?
最佳答案
只需将 selfcontained = TRUE
添加到最后一行:
htmlwidgets::saveWidget(as_widget(plotly), selfcontained = TRUE, file = "xxxx/graph.html")
关于R图表转换为html格式,无需其他文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48497704/