Search code examples
rplotlybar-chartr-plotly

R plotly bar chart positive/negative values different color second axis


So from this post Im trying to make a plotly bar chart on the second y-axis with negative values depicted as red and posiive values as green....but somehow the colors are still wrong....why?

    t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
              "two" = c(-5,6,9,-8,7,-1,5,2,1),
              "three" = c(2,5,6,9,8,7,8,4,5))
  
  Plot <- 
    
    plot_ly(t, x = ~one, y = ~three, type = 'scatter',  mode = 'lines',
            name = "three", line = list(color = "#ffc107")
    ) 
  
  Plot <- Plot %>% 
    add_trace(x = ~one, y = ~two, type = 'bar', name = "two",   
              color = ~two < 0, colors = c("#28a745", "#dc3545"),
              yaxis = 'y2'
    ) %>% 
    
    layout(title = "test",
           xaxis = list(titel = "one"), 
           yaxis = list(side = 'left', title = 'two', 
                        showgrid = F, zeroline = F,
                        showline = T),
           yaxis2 = list(side = 'right', overlaying = "y", 
                         title = 'three', 
                         showgrid = F, zeroline = F, 
                         showline = T))

enter image description here


Solution

  • The definition of colors in the plot_ly() call (NULL in your example) is recycled in add_trace() if you don't prevent it.

    Please check the following:

    library(plotly)
    library(dplyr)
    
    t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
                "two" = c(-5,6,9,-8,7,-1,5,2,1),
                "three" = c(2,5,6,9,8,7,8,4,5))
    
    # 'bar' objects don't have these attributes: 'mode', 'line'
    Plot <- plot_ly(t, x = ~one, y = ~two, type = 'bar', name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),   
                color = ~two < 0, colors = c("#28a745", "#dc3545"),
                yaxis = 'y'
      ) %>% add_trace(t, x = ~one, y = ~three, type = 'scatter',  mode = 'lines',
                  name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y2"
                  
      ) %>% layout(title = "test",
             xaxis = list(titel = "one"),
             yaxis = list(side = 'right', 
                           title = 'three', 
                           showgrid = F, zeroline = F, 
                           showline = T),
             yaxis2 = list(side = 'left', title = 'two', 
                           showgrid = F, zeroline = F,
                           showline = T, overlaying = "y"))
    
    Plot
    

    result


    Edit as requested below:

    library(plotly)
    library(dplyr)
    
    t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
                "two" = c(-5,6,9,-8,7,-1,5,2,1),
                "three" = c(2,5,6,9,8,7,8,4,5))
    
    # 'bar' objects don't have these attributes: 'mode', 'line'
    Plot <- plot_ly(t, x = ~one, y = ~three, type = 'scatter',  mode = 'lines',
                    name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y") %>%
      add_trace(t, x = ~one, y = ~two, type = 'bar', mode = NULL, line = NULL, name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),   
                     color = ~two < 0, colors = c("#28a745", "#dc3545"),
                     yaxis = 'y2') %>%
      layout(title = "test",
                 xaxis = list(titel = "one"),
                 yaxis = list(side = 'right', 
                              title = 'three', 
                              showgrid = F, zeroline = F, 
                              showline = T),
                 yaxis2 = list(side = 'left', title = 'two', 
                               showgrid = F, zeroline = F,
                               showline = T, overlaying = "y"),
             hovermode = "x unified")
    
    Plot