r绘制条形图正值/负值不同颜色的第二轴

编程入门 行业动态 更新时间:2024-10-23 21:39:57
本文介绍了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绘制条形图正值/负值不同颜色的第二轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-13 17:01:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:负值   不同颜色   条形图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!