格式化悬停数据标签Plotly R

Mav*_*vic 5 r ggplotly r-plotly

我试图格式化当我将鼠标悬停在使用Plotly创建的图表的一部分上时出现的数据标签。标签目前正以这种方式显示。我希望标签仅显示利润。

我创建情节的代码是:

output$monthlyProfits <- renderPlotly({
ggplot(profitTemp, aes(x = Date, y = Profit)) + geom_line(aes(text=Profit), 
colour = "Blue") 
Run Code Online (Sandbox Code Playgroud)

如何设置数据标签的格式,使其不显示X轴,而仅显示Y轴(利润)?我尝试过,aes(text=Profit)但X轴仍然显示。

任何帮助将不胜感激。

mis*_*use 7

It is more flexible to customize the plots that are directly made in plotly, however the requested operation is also possible using ggplotly. Here is an example on the iris data set:

library(plotly)
library(ggplot)
Run Code Online (Sandbox Code Playgroud)

To define the hover info:

plot_ly(data = iris,
        x = ~Sepal.Length,
        y = ~Petal.Length,
        color = ~Species,
        hoverinfo = 'text',
        text = ~Species)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

to do so with ggplotly leave the text argument blank when calling ggplot:

z <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))+
  geom_point() 
Run Code Online (Sandbox Code Playgroud)

and set the argument tooltip to ggplotly:

ggplotly(z, tooltip="Species")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

compared to:

z <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))+
  geom_point(aes(text = Species)) 

ggplotly(z)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

EDIT: custom text:

plot_ly(data = iris,
        x = ~Sepal.Length,
        y = ~Petal.Length,
        color = ~Species,
        hoverinfo = 'text',
        text = ~paste(Species,
                      '</br></br>', Petal.Length))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明