How to choose variable to display in tooltip when using ggplotly?

前端 未结 4 1234
[愿得一人]
[愿得一人] 2020-11-28 22:01

I have a simple data frame:

seq <- 1:10
name <- c(paste0(\"company\",1:10))
value <- c(250,125,50,40,40,30,20,20,10,10)
d <- data.frame(seq,name,         


        
4条回答
  •  我在风中等你
    2020-11-28 22:27

    You'll have to modify the plotly object to do this. Or use plot_ly() to create the graph instead...

    EDIT: With the release of plotly 4.0 the syntax will change a little bit.

    seq <- 1:10
    name <- c(paste0("company",1:10))
    value <- c(250,125,50,40,40,30,20,20,10,10)
    d <- data.frame(seq,name,value)
    
    require(plotly)
    gg <- ggplot(data = d,aes(x=seq,y=value))+geom_line() + geom_point()
    
    gg <- plotly_build(gg)
    
    #OLD:
    gg$data[[1]]$text <- paste("Seq:", d$seq, "
    ", "Value:", d$value, "
    ", "Company:", d$name) #UPDATED: #Plotly_build creates two separate traces: #One with mode = markers and the other with mode = lines #Hence modify text for the second trace gg$x$data[[2]]$text <- paste("Seq:", d$seq, "
    ", "Value:", d$value, "
    ", "Company:", d$name) gg

提交回复
热议问题