【问题标题】:R plotly bar chart positive/negative values different color second axisR plotly条形图正/负值不同颜色第二轴
【发布时间】:2022-01-04 09:29:02
【问题描述】:

所以从this post 我试图在第二个 y 轴上制作一个绘图条形图,负值表示为红色,正值表示为绿色....但不知何故颜色仍然是错误的....为什么?

    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))

【问题讨论】:

    标签: r plotly bar-chart r-plotly


    【解决方案1】:

    plot_ly() 调用中colors 的定义(在您的示例中为NULL)如果您不阻止它,将在add_trace() 中回收。

    请检查以下内容:

    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
    


    编辑如下要求:

    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
    

    【讨论】:

    • 谢谢!那么还有一种方法可以先确定折线,然后再确定条形图吗?因为现在当我在布局中通过 hovermode = "x Unified" 将鼠标悬停在绘图上时,首先给出条形图值,然后给出第二行值
    • 当然可以。请看我的编辑。但是,这条线被条形部分覆盖,在我看来,这似乎不利,反之亦然。
    • 是的,为什么调用“overlaying = n”不起作用? ...此外,我确实收到此错误:警告消息:1:在 RColorBrewer::brewer.pal(N, "Set2") 中:n 的最小值为 3,返回具有 3 个不同级别的请求调色板 2:在 RColorBrewer:: brewer.pal(N, "Set2") : n 的最小值为 3,返回请求的具有 3 个不同级别的调色板
    • 请用适当的例子提出一个新问题并将其链接到此处,而不是尝试为此添加新方面。
    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 2020-05-16
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2017-01-21
    相关资源
    最近更新 更多