【问题标题】:How to make histogram bars to have different colors in Plotly in R如何在 R 中的 Plotly 中使直方图条具有不同的颜色
【发布时间】:2019-07-30 14:31:11
【问题描述】:

我正在使用 R 中的 plotly 包制作直方图,下面是图表。

这是我的代码:

plot_ly(test, x = ~count, type = 'histogram',  marker = list(color = 'blue'))

我想知道是否有一种方法可以使图表从颜色角度(例如,每个条形的不同颜色)更直观,如下图所示。

这是我在网上找到的一个例子:

提前致谢。

【问题讨论】:

    标签: r ggplot2 r-plotly


    【解决方案1】:
    plot_ly(test, x = ~count, color = ~count) %>%
      add_histogram()
    

    【讨论】:

      【解决方案2】:
      plot_ly(test, x = ~ count, type = 'histogram',
              marker = list(color = viridis::viridis_pal(option = "C", direction = -1)(4)))
      

      【讨论】:

      • 谢谢。在直方图上,如果我扩大 bin 的数量,x 轴将跳过数字。例如,我将 bin 的数量从 4 增加到 11。但是,x 轴仅显示 2、4、6 等,而跳过了 1、3、5 等。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2016-12-13
      • 2020-08-30
      • 2017-04-27
      • 2018-12-30
      • 2021-11-04
      • 2020-11-09
      • 1970-01-01
      • 2012-04-25
      相关资源
      最近更新 更多