Search code examples
rplotly

Skip irelevant text from some of bars in plotly bar chart


I create a simple plotly bar chart but inside the first columns I get a weird text that I need to delete.

library(plotly)
sum2<-structure(list(month = structure(c(1451606400, 1454284800, 1456790400, 
1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 1472688000, 
1475280000), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    Monthly = c(14236.895, 4519.892, 55691.009, 28295.345, 23648.287, 
    34595.1276, 33946.393, 27909.4685, 81777.3508, 31453.393)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

fig <- plot_ly(sum2, x = ~month, y = ~Monthly, type = 'bar', text = text,
               marker = list(color = 'rgb(158,202,225)',
                             line = list(color = 'rgb(8,48,107)',
                                         width = 1.5))
)
fig <- fig %>% layout( width=1800,height=600
)

fig

Solution

  • Not sure what you were doing with text = text but just remove that line and it looks fine.

    plot_ly(
      sum2,
      x = ~ month,
      y = ~ Monthly,
      type = 'bar',
      # text = text,
      marker = list(
        color = 'rgb(158,202,225)',
        line = list(color = 'rgb(8,48,107)',
                    width = 1.5)
      )
    )
    

    enter image description here


    This happens because text is referring to the function graphics::text() and when called without parentheses it just gives the report of that object with that name. Try just running text at the console (without parentheses) and you'll see what was getting passed as labels to your plot.

    text
    #> function (x, ...) 
    #> UseMethod("text")
    #> <bytecode: 0x0000000013994f20>
    #> <environment: namespace:graphics>
    

    Created on 2022-02-07 by the reprex package (v2.0.1)