我正在尝试设置当我将鼠标悬停在使用Plotly创建的图表的一部分上时显示的数据标签的格式。该标签当前显示为类似this。我希望标签只显示利润。
我用来创建绘图的代码是:
output$monthlyProfits <- renderPlotly({
ggplot(profitTemp, aes(x = Date, y = Profit)) + geom_line(aes(text=Profit),
colour = "Blue") 如何设置数据标签的格式,使其不显示X轴而仅显示Y轴(利润)?我尝试过使用aes(text=Profit),但X轴仍然显示。
任何帮助都将不胜感激。
发布于 2018-03-21 19:38:55
直接在plotly中自定义绘图更加灵活,但是也可以使用ggplotly执行所请求的操作。下面是一个关于虹膜数据集的例子:
library(plotly)
library(ggplot)定义悬停信息的步骤:
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Petal.Length,
color = ~Species,
hoverinfo = 'text',
text = ~Species)

要使用ggplotly执行此操作,请在调用ggplot时将text参数留空:
z <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))+
geom_point() 并将参数tooltip设置为ggplotly
ggplotly(z, tooltip="Species")

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

编辑:自定义文本:
plot_ly(data = iris,
x = ~Sepal.Length,
y = ~Petal.Length,
color = ~Species,
hoverinfo = 'text',
text = ~paste(Species,
'</br></br>', Petal.Length))

发布于 2018-05-29 14:11:02
关于你在评论中的第二个问题(很抱歉没有留下评论-没有这样做的声誉):
只需向tooltip属性提供一个向量,例如
ggplotly(produceMonthlyPlot, tooltip=c("QuantitySold","Product"))这样你就可以控制应该显示什么,不应该显示什么。
https://stackoverflow.com/questions/49404394
复制相似问题