从悬停文本中删除列名称
Remove column name from hover text
如何删除下图悬停文本中的 cyl:
?
library(plotly)
ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(),
tooltip = c("colour"))
可能有更优雅的方法,但您可以尝试:
b <- ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(),
tooltip = c("colour"))
p <- plotly_build(b)
p$data[[1]]$text
library(stringr)
p$data[[1]]$text <- str_sub(p$data[[1]]$text,-2,-1)
p
您只需将鼠标悬停时显示的文本替换为您想要的其他内容。在您的示例中,我只是提取了最后一位数字。
您可以使用 text
美学来显示不带列名称的值:
library(plotly)
ggplotly(ggplot(
mtcars,
aes(mpg, hp, colour = cyl, text = cyl)) + geom_point(),
tooltip = c("text")
))
您还可以使用 text
来粘贴字符串或样式,而无需此代码显示为 "column name":
ggplotly(ggplot(
mtcars,
aes(mpg, hp, colour = cyl, text = paste("Cylinders: ", cyl)) +
geom_point(),
tooltip = c("text")
))
它没有很好的记录,但请参阅 here 了解更多信息。
如何删除下图悬停文本中的 cyl:
?
library(plotly)
ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(),
tooltip = c("colour"))
可能有更优雅的方法,但您可以尝试:
b <- ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(),
tooltip = c("colour"))
p <- plotly_build(b)
p$data[[1]]$text
library(stringr)
p$data[[1]]$text <- str_sub(p$data[[1]]$text,-2,-1)
p
您只需将鼠标悬停时显示的文本替换为您想要的其他内容。在您的示例中,我只是提取了最后一位数字。
您可以使用 text
美学来显示不带列名称的值:
library(plotly)
ggplotly(ggplot(
mtcars,
aes(mpg, hp, colour = cyl, text = cyl)) + geom_point(),
tooltip = c("text")
))
您还可以使用 text
来粘贴字符串或样式,而无需此代码显示为 "column name":
ggplotly(ggplot(
mtcars,
aes(mpg, hp, colour = cyl, text = paste("Cylinders: ", cyl)) +
geom_point(),
tooltip = c("text")
))
它没有很好的记录,但请参阅 here 了解更多信息。