【问题标题】:How to change mjs_histogram bar color (using metricsgraphics r package)?如何更改 mjs_histogram 条颜色(使用 metricsgraphics r 包)?
【发布时间】:2016-07-29 15:08:17
【问题描述】:

我似乎无法弄清楚如何更改使用 metricsgraphics 包创建的直方图的颜色。我已经构建了一个功能正常的 Shiny 应用程序,它使用以下代码呈现直方图:

mjs_plot(zedata()$Value, format="count") %>% 
        mjs_histogram(bins = 10) %>%
        mjs_labs(x=input$item, y="Number of VA Medical Centers")

我添加了 color = "#d7191c" mjs_plot 和 mjs_histogram 无济于事 - 我在这两种情况下都遇到了未使用的参数错误。我在 hrbrmstr 的信息页面 http://hrbrmstr.github.io/metricsgraphics/ 上找不到任何内容,在帮助手册中也找不到任何内容。似乎对除直方图之外的每种图形类型都解释了使用颜色选项。

我不擅长 html/javascript,不知道还能尝试什么...

【问题讨论】:

    标签: r shiny htmlwidgets metricsgraphicsjs


    【解决方案1】:

    您必须修改与直方图矩形对应的类的 CSS(在 original CSS 中查找类的名称)。

    一个简单的方法是将以下代码添加到您的 UI 定义中:

    tags$head(
      tags$style(HTML("
        .mg-histogram .mg-bar rect {
            fill: <your_color>;
            shape-rendering: auto;
        }
    
        .mg-histogram .mg-bar rect.active {
            fill: <another_color>;
        }")))
    

    还有其他添加自定义 CSS 的方法,请参阅 here

    这是一个完整的例子:

    n <- 5
    library(metricsgraphics)
    library(shiny)
    
    # Define the UI
    ui <- bootstrapPage(
      tags$head(
        tags$style(HTML("
          .mg-histogram .mg-bar rect {
              fill: #ff00ff;
              shape-rendering: auto;
          }
    
          .mg-histogram .mg-bar rect.active {
              fill: #00f0f0;
          }"))),
      numericInput('n', 'Number of obs', n),
      metricsgraphicsOutput('plot')
    )
    
    server <- function(input, output) {
      output$plot <- renderMetricsgraphics({
        mjs_plot(mtcars$mpg, format="count") %>% 
          mjs_histogram(bins = input$n)
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-12
      • 2014-10-08
      • 2011-07-07
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多