【问题标题】:Echarts4r : Conditional Color in Bar ChartEcharts4r:条形图中的条件颜色
【发布时间】:2019-08-21 06:55:18
【问题描述】:

我正在制作一个提供销售业绩的仪表板。我正在使用echarts4r,并部署在shinyapps.io 的云中

datafpytd[datafpytd$NIK==input$NIK,] %>% 
  e_charts(Month) %>% 
  e_bar(serie = GAP_Revenue, name = "Gap Revenue YTD") %>%
  e_color(ifelse(datafpytd[datafpytd$NIK==input$NIK,]$GAP_Revenue>0,"green","red"))

如何使柱状图的颜色根据其值不同,如果大于0,颜色应该是绿色,否则应该是红色?

这是示例数据:

 structure(list(Index = c(1L, 1L, 1L, 1L, 1L, 1L), Group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Major Accounts", class = "factor"), 
    Div = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Financial Institution", class = "factor"), 
    Dept = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "-", class = "factor"), 
    Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Jamil W.", class = "factor"), 
    Status = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "PE", class = "factor"), 
    NIK = c(75994631L, 75994631L, 75994631L, 75994631L, 75994631L, 
    75994631L), Data_Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = "Monthly", class = "factor"), Month = structure(c(3L, 
    2L, 5L, 1L, 6L, 4L), .Label = c("April", "February", "January", 
    "June", "March", "May"), class = "factor"), Name_FP = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "Financial Institution - Jamil W.", class = "factor"), 
    FAB_In = c(681272250L, 549260036L, 247058501L, 492335484L, 
    1624053867L, 636687867L), Target_FAB_In = c(562081784L, 655762082L, 
    708223048L, 929889299L, 1033210332L, 797047970L), Revenue = c(32594192621, 
    31953999491, 31259721093, 31809774195, 31725823061, 29965576806
    ), Target_Revenue = c(31609626147, 32896422576, 34077352579, 
    31709041552, 32174547049, 33854985985)), class = "data.frame", row.names = c(NA, 
-6L)) 

谢谢

【问题讨论】:

  • 你能放一个dput()的样本数据吗?
  • 嗨,我不知道如何在此处附加数据,但我使用的是 csv 数据,GAP 收入包含一个数字,月份是一个月。
  • 在你的电脑上运行dput(head(data,10)),复制输出并放在这里。
  • 这是数据,发布在问题中。谢谢顺便说一句:)
  • 哇,它成功了@maydin! thaaanks 战利品! :)

标签: r shiny shinydashboard echarts


【解决方案1】:

我是 echarts4r 的作者,很抱歉这么晚才看到你的问题。您可以使用e_visual_map 并将其设置为分段。

df <- tibble::tibble(
  Month = as.character(1:10),
  Revenue = runif(10, -10, 10)
)

library(echarts4r)

df %>% 
  e_charts(Month) %>% 
  e_bar(Revenue) %>% 
  e_visual_map(
    type = "piecewise",
    pieces = list(
      list(
        gt = 0,
        color = "green"
      ),
      list(
        lte = 0,
        color = "red"
      )
    )
  )

【讨论】:

    【解决方案2】:

    为了显示其他选项并提供潜在的替代方案echarts4r,使用@JohnCoene 的示例,我们有:

    df <- tibble::tibble(
      Month = as.character(1:10),
      Revenue = runif(10, -10, 10)
    )
    
    library(echarts4r)
    library(dplyr)
    
    df %>% 
      dplyr::mutate(cond = ifelse(Revenue > 0, "green", "red")) %>% 
      dplyr::group_by(cond) %>% 
      e_charts(Month) %>% 
      e_bar(Revenue) %>% 
      e_color(color = c("green", "red")) %>% 
      e_legend(FALSE)
    

    还有一个选择。

    df %>% 
      e_charts(Month) %>% 
      e_bar(Revenue,
            itemStyle = list(color = htmlwidgets::JS("
              function(params) {
                                    var colorList = ['blue', 'blue', 'pink', 'blue', 'pink', 'blue', 'pink', 'blue', 'blue', 'pink'];
                                    return colorList[params.dataIndex]
                                    }
                                    
        "))) %>% 
      e_legend(FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2012-01-19
      • 2020-08-10
      • 2013-02-11
      • 2019-06-11
      • 1970-01-01
      相关资源
      最近更新 更多