将 html id 属性分配给 ggplotly 图
有没有办法将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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. classplot-container
ormain-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.