为情节图例添加标题 [英] Add title to the plotly legend

查看:41
本文介绍了为情节图例添加标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下示例中,我如何为 R 的 plot_ly 中的图例添加标题?

In the following example how can i add a title to the legend in plot_ly for R ?

mtcars %>%   plot_ly(x = ~disp, y = ~mpg, color = ~factor(cyl), size = ~wt) %>%   add_markers(
    hoverinfo = "text",
    text = ~paste("Displacement = ", disp, "\nMiles Per Gallon = ", mpg)   ) %>%   layout(title ="Custom Hover Text")

谢谢

推荐答案

我知道的唯一方法是使用注释并将其添加到绘图中.像这样:

The only way I know is to use an annotation and add it to the plot. Like this:

legendtitle <- list(yref='paper',xref="paper",y=1.05,x=1.1, text="Cylinders",showarrow=F)

mtcars %>%  plot_ly(x = ~disp, y = ~mpg, color = ~factor(cyl), size = ~wt) %>%   
  add_markers(  hoverinfo = "text",
                text = ~paste("Displacement=",disp, "\nMiles Per Gallon = ", mpg)) %>%   
  layout(title ="Custom Hover Text", annotations=legendtitle )

产量:

虽然放置图例标题有点棘手,不确定这个放置是否总是有效.

It is a bit tricky to place the legend title though, not sure if this placement would always work.

另一种方法当然是使用 ggplot 和 ggplotly,然后让 ggplot 弄清楚.

Another way would be to use ggplot and ggplotly of course, and let ggplot figure it out.

这篇关于为情节图例添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆