【问题标题】:Plotly - How to change the Histogram colour?Plotly - 如何更改直方图颜色?
【发布时间】:2018-12-30 05:58:30
【问题描述】:

我正在尝试绘制具有特定颜色的直方图,因为目的是从两个不同的数据帧创建两个直方图,并且我不想将它们都显示为默认的蓝色。我知道解决方案意味着将 ggplo2 对象转换为 plotly,但我想找到一种方法来解决 plotly 代码中的这个小问题。 基本绘图直方图的代码如下:

    plot_ly(x=~dataframe$variable, type="histogram") %>%
    layout(title="Histogram title", xaxis=list(title="X-axis title"))

我尝试过的两种解决方案都不起作用:

1) 第一次尝试:

    plot_ly(x=~dataframe$variable, type="histogram", color="green") %>%
    layout(title="Histogram title", xaxis=list(title="X-axis title"))

它返回以下警告消息:

    In RColorBrewer::brewer.pal(N, "Set2") :
    minimal value for n is 3, returning requested palette with 3 different levels

2) 第二次尝试:

    plot_ly(x=~dataframe$variable, type="histogram", colour="green") %>%
    layout(title="Histogram title", xaxis=list(title="X-axis title"))

它返回以下警告消息:

    'histogram' objects don't have these attributes: 'colour'
    Valid attributes include:
    'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'hoverinfo', 'hoverlabel', 'stream', 'x', 'y', 'text', 'orientation', 'histfunc', 'histnorm', 'cumulative', 'autobinx', 'nbinsx', 'xbins', 'autobiny', 'nbinsy', 'ybins', 'marker', 'error_y', 'error_x', '_deprecated', 'xaxis', 'yaxis', 'xcalendar', 'ycalendar', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule'

有什么建议吗?

【问题讨论】:

  • 嗯,同样的问题已经在以下超链接中描述:stackoverflow.com/questions/38740837/…。一般来说,“ColorBrewer”会出现问题,因为数据类的最小数量是三个。
  • 感谢丹尼尔的回答,但直方图默认仍为蓝色,即使我尝试将绘图输出分配给对象并使用 suppressWarnings() 函数。发布的超链接与隐藏警告消息的最佳方法有关,而不是更改 plotly 直方图的颜色

标签: r colors histogram plotly


【解决方案1】:

下面的情节有效吗? color 参数必须指定给marker 属性。

library(plotly)
set.seed(1)
dataframe <- data.frame(variable = rnorm(1000))
plot_ly(x=~dataframe$variable, type="histogram", marker = list(color = 'green')) %>%
  layout(title="Histogram title", xaxis=list(title="X-axis title"))

【讨论】:

  • 非常感谢 Ameya,我在 plotly 上找到了有关其他图表的许多详细信息,但不幸的是我错过了直方图上的这些信息。现在可以正常工作了,谢谢一百万!
猜你喜欢
  • 2018-02-17
  • 2021-12-25
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 2018-08-27
相关资源
最近更新 更多