R绘图条形图正/负值不同颜色第二轴

标签 r plotly bar-chart r-plotly

所以来自 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))

enter image description here

最佳答案

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

result


编辑按照以下要求:

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

关于R绘图条形图正/负值不同颜色第二轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70576618/

相关文章:

R:在单个因子变量上加宽多个列

r - 如何在 R 中进行部分替换?

r - 从 rlang 0.3.0 和 mutate_impl 起已失效

python - Plotly 中同一 X 轴和 Y 轴上的多个条形图

r - 如何在R中使用plotly水平居中饼图?

r - 如何导出列分割的子集?

r - 在散点图中按因子分割点

c++ - 如何使用 Blackberry 10 Native 方法生成条形图

r - 按第二个变量分组的 ggvis 并排条形图

R - stackplot/barplot 随时间变化的比例