在下面的示例中,如何在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")
感谢
答案 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 )
产量:
虽然放置图例标题有点棘手,但不确定此放置是否始终有效。
另一种方法是使用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