r-如何自定义ggplotly的工具提示



我试着在这里找到不同的答案,但都没有成功。我仔细查看了官方文件,得出了以下结论:

数据

以下是数据集的示例:

> 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.name显示,即BlueT5。我希望它对所有ADO.name都可见。这里有什么问题?

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

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

另请参阅:使用ggplotly时,如何选择要在工具提示中显示的变量?

最新更新