【问题标题】:How to customize the tooltip of ggplotly?如何自定义ggplotly的工具提示?
【发布时间】:2016-02-27 12:36:48
【问题描述】:

我试图在这里遵循不同的答案,但没有一个奏效。我浏览了 plotly 官方文档并得出以下结论:

数据

以下是数据集的样本:

> dput(head(df))
structure(list(ID = c(-1, -1, -1, -1, -1, -1), spacing.ft = c(0, 
0, 0, 0, 0, 0), gap.s = c(0, 0, 0, 0, 0, 0), frspacing.ft = c(0, 
0, 0, 0, 0, 0), TTC = c(0, 0, 0, 0, 0, 0), LV.vel.fps = c(0, 
0, 0, 0, 0, 0), x = c(0, 0, 0, 0, 0, 0), y = c(0, 0, 0, 0, 0, 
0), z = c(0, 0, 0, 0, 0, 0), frames = 29373:29378, df16 = c(6L, 
6L, 6L, 6L, 6L, 6L), ADO.name = structure(c(NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_), .Label = c("BlueT5", 
"ghtTFrei10", "ilT6Carg", "owT8Yell", "CargoT4", "MoveT12", "RaceT11", 
"RedT1", "SemiT3", "StarT7", "WhiteT2", "artTWalm9"), class = "factor"), 
    speed.fps.ED = c(33.25, 33.4, 33.55, 33.7, 33.84, 33.99), 
    deltaV.fps = c(33.25, 33.4, 33.55, 33.7, 33.84, 33.99)), .Names = c("ID", 
"spacing.ft", "gap.s", "frspacing.ft", "TTC", "LV.vel.fps", "x", 
"y", "z", "frames", "df16", "ADO.name", "speed.fps.ED", "deltaV.fps"
), row.names = c(NA, 6L), class = "data.frame")

我想做什么:

我想自定义工具提示以增加速度,speed.fps.ED。我试过以下:

library(ggplot2)
library(plotly)
mt.plot <-  ggplot() + 
  geom_point(data = df,
             mapping = aes(x = deltaV.fps, y = frspacing.ft, color = ADO.name))

# Build the ggplot:
p <- plotly_build(mt.plot)


# Change the tooltip:
p$data[[1]]$text <- paste("ED.speed = ", df$speed.fps.ED)



p$filename <- 'test'
r <- plotly_POST(p)
knit_print.plotly(r, options=list())  

您可以在此处查看结果图:Plot

问题

问题是工具提示中的第三个元素仅显示为 1 ADO.nameBlueT5。我希望它对所有ADO.names 都可见。这里有什么问题?

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    您可以将speed.fps.ED 添加到ggplot 美学中,如下所示:

    geom_point(data = df,
      aes(x = deltaV.fps, y = frspacing.ft, color = ADO.name, label = speed.fps.ED))
    

    另请参阅:how to choose variable to display in tooltip when using ggplotly?

    【讨论】:

      猜你喜欢
      • 2021-02-07
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2013-06-23
      相关资源
      最近更新 更多