3
votes

I have tried to follow different answers here but none worked. I went through the plotly official documentation and came up with following:

Data

Following is a sample of the data set:

> 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")

What I want to do:

I want to customize the tooltip to add speed, speed.fps.ED. I tried following:

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())  

You can see the resulting plot here: Plot.

Problem

The problem is that the third element in the tooltip is displayed only for 1 ADO.name i.e. BlueT5. I want it to be visible for all ADO.names. What is the problem here?

1

1 Answers

3
votes

You can add speed.fps.ED to the ggplot aesthetic, as in:

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

See also: how to choose variable to display in tooltip when using ggplotly?