为情节图例添加标题

时间:2016-10-12 14:41:17

标签: r plot plotly

在下面的示例中,如何在plot_ly中为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")

感谢

2 个答案:

答案 0 :(得分:3)

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

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 )

产量:

enter image description here

虽然放置图例标题有点棘手,但不确定此放置是否始终有效。

另一种方法是使用ggplot和ggplotly,然后让ggplot计算出来。

答案 1 :(得分:3)

此功能此后已包含在layout选项的legend函数中。有一个名为title的子选项,您可以在其中提供包含文本的列表。

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", 
         legend = list(title = list(text = "<b>Cylinders</b>"))) # TITLE HERE
相关问题