Search code examples
rshinyplotlyscatter-plot

How to remove trace from the plot


I want to remove NA trace from the plot and the legend below.

  plot_ly(data = bData,  x = ~`Maturity Date`, y =  ~YVal,  type = 'scatter', mode='markers',  
              symbol = ~Sym,  symbols = c('circle-open','x-open','diamond-open') ,
              text = ~paste(" Security: ", bData$Security, "<br>",
                            "Currency: ", bData$Crncy, "<br>",
                            "YTM: ", bData$YTM,"<br>",
                            "DM: ", bData$DM) ,
              hoverinfo = 'text',legendgroup = 'group1'
      ) %>%
        add_trace(x = ~`Maturity Date`, y =  ~YVal , symbol=~Crncy,legendgroup = 'group2')

enter image description here


Solution

  • Try this. You can isolate data without any value in a new dataframe and then plot it. In this example I will omit the None value in Sym variable but if you have NA use !is.na() as I also added in a new line of code for you. Here the code:

    library(plotly)
    #New data example
    bData2 <- bData[bData$Sym!='None',]
    #For your real data
    bData2 <- bData[!is.na(bData$Sym),]
    #Code
    plot_ly(data = bData2,  x = ~`Maturity Date`, y =  ~YVal,  type = 'scatter', mode='markers',  
            symbol = ~Sym,  symbols = c('circle-open','x-open','diamond-open') ,
            text = ~paste(" Security: ", bData2$Security, "<br>",
                          "Currency: ", bData2$Crncy, "<br>",
                          "YTM: ", bData2$YTM,"<br>",
                          "DM: ", bData2$DM) ,
            hoverinfo = 'text',legendgroup = 'group1'
    ) %>%
      add_trace(x = ~`Maturity Date`, y =  ~YVal , symbol=~Crncy,legendgroup = 'group2')
    

    Output omitting None:

    enter image description here

    Update: Try this hack, replace the values belonging to the None class with NA, transforming first the factor to character and then again come back to factor. Real NA will be dropped by plotly functions. Here the code:

    #Replace
    bData$Sym <- as.character(bData$Sym)
    bData$Sym[bData$Sym=='None']<-NA
    bData$Sym <- as.factor(bData$Sym)
    #Code
    plot_ly(data = bData,  x = ~`Maturity Date`, y =  ~YVal,  type = 'scatter', mode='markers',  
            symbol = ~Sym,  symbols = c('circle-open','x-open','diamond-open') ,
            text = ~paste(" Security: ", bData$Security, "<br>",
                          "Currency: ", bData$Crncy, "<br>",
                          "YTM: ", bData$YTM,"<br>",
                          "DM: ", bData$DM) ,
            hoverinfo = 'text',legendgroup = 'group1'
    ) %>%
      add_trace(x = ~`Maturity Date`, y =  ~YVal , symbol=~Crncy,legendgroup = 'group2')
    

    Output:

    enter image description here

    Or also try this, duplicating YVal and setting NA for the first variable:

    #Replace 2
    bData$YVal2 <- bData$YVal
    bData$Sym <- as.character(bData$Sym)
    bData$Sym[bData$Sym=='None']<-NA
    bData$YVal[is.na(bData$Sym)]<-NA
    bData$Sym <- as.factor(bData$Sym)
    #Code
    plot_ly(data = bData,  x = ~`Maturity Date`, y =  ~YVal,  type = 'scatter', mode='markers',  
            symbol = ~Sym,  symbols = c('circle-open','x-open','diamond-open') ,
            text = ~paste(" Security: ", bData$Security, "<br>",
                          "Currency: ", bData$Crncy, "<br>",
                          "YTM: ", bData$YTM,"<br>",
                          "DM: ", bData$DM) ,
            hoverinfo = 'text',legendgroup = 'group1'
    ) %>%
      add_trace(x = ~`Maturity Date`, y =  ~YVal2 , symbol=~Crncy,legendgroup = 'group2')
    

    Output:

    enter image description here