如何自定义ggplotly的工具提示?

时间:2015-11-24 21:40:00

标签: r ggplot2 plotly

我试图在这里遵循不同的答案但没有效果。我仔细阅读了官方文档并提出了以下内容:

数据

以下是数据集的示例:

CREATE TABLE my_table (
    ...
    shop_group_name VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci
);

我想做什么:

我想自定义工具提示以添加速度> 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

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

问题

问题是工具提示中的第三个元素仅显示1 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()) ,即 BlueT5 。我希望所有ADO.name都能看到它。这有什么问题?

1 个答案:

答案 0 :(得分:2)

您可以将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?