2016-04-14 106 views
2

如何从下面的图中删除cyl:的地址?从悬停文本中删除列名

library(plotly) 
ggplotly(ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point(), 
     tooltip = c("colour")) 

enter image description here

+0

怎么试着看这个[link](https://stackoverflow.com/questions/34108350/disable-hover-text-in-plotly-with-ggplot?rq=1)。也许它会有所帮助 –

回答

1

有可能是一个更优雅的方式来做到这一点,但你可以尝试:

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 

你只需要替换的文字表明,当你通过别的悬停在您想。在你的例子中,我只是提取最后一位数字。

0

可以使用text审美没有列名的显示值:

library(plotly) 
ggplotly(ggplot(
    mtcars, 
    aes(mpg, hp, colour = cyl, text = cyl)) + geom_point(), 
    tooltip = c("text") 
)) 

您还可以使用text在字符串或造型粘贴,没有出现为“列名”这样的代码:

ggplotly(ggplot(
    mtcars, 
    aes(mpg, hp, colour = cyl, text = paste("Cylinders: ", cyl)) + 
    geom_point(), 
    tooltip = c("text") 
)) 

它没有非常好的记录,但请参阅here了解更多信息。