将 html id 属性分配给 ggplotly 图

发布于 2025-01-17 19:45:03 字数 303 浏览 3 评论 0原文

有没有办法将HTML ID属性分配给通过ggplotly生成的图(并在rmarkDown中渲染)?

我正在寻找这样的东西:

df <- data.frame(x = 1:3, y = 1:3)
gg <- ggplot(df, aes(x, y)) + geom_point()
ggplotly(gg, id = "myid")

这会引发错误,因为此功能没有ID参数。

Is there a way to assign a HTML id attribute to a plot generated via ggplotly (and rendered in Rmarkdown)?

I am looking for something like this:

df <- data.frame(x = 1:3, y = 1:3)
gg <- ggplot(df, aes(x, y)) + geom_point()
ggplotly(gg, id = "myid")

This throws an error as there is not an id argument for this function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2025-01-24 19:45:03

ggplotly似乎有节俭的ID分配(除了顶级HTMLWIDGETS容器除外),但是它确实使用了绘图组件的各种特定CSS类(例如类Plot> plot-plot-container 或MAIN-SVG)。您可以使用浏览器文档Inspecor识别这些类(使用F12打开并激活选择器工具)。

只要您的页面上只有一个ggplotly输出,该类可能与CSS选择的ID一样有用,例如$(。main-svg)而不是<<代码> $(#myid)。对于更多对象,这将是一些索引试验和错误。

ggplotly seems to be frugal with ID assignments (except for the top level htmlwidgets container), but it does use a variety of specific css classes for the plot components (e.g. class plot-container or main-svg). You can identify these classes with your browsers document inspector (open with F12 and activate the selector tool).

As long as there's only one ggplotly output on your page, the class might be as useful as an ID for CSS selection, e.g.$(.main-svg) instead of $(#myid). For more objects, it'll be some indexing trial-and-error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文