本文介绍了r绘制条形图正值/负值不同颜色的第二轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,从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))

推荐答案

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

这篇关于r绘制条形图正值/负值不同颜色的第二轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 09:45