Plotly - 如何更改直方图的颜色?

5
我正在尝试绘制一个带有特定颜色的直方图,因为目的是从两个不同的数据框创建两个直方图,并且我不想用默认的蓝色呈现它们两个。我知道解决方案将涉及将ggplo2对象转换为plotly,但我想找到一种在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

第二次尝试:
    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'

任何建议?

好的,同样的问题已经在以下链接中描述了:https://dev59.com/Ppnga4cB1Zd3GeqPZoJV#38744227。一般来说,这个问题出现在“ColorBrewer”中,因为最小的数据类别数量是三个。 - Daniel
谢谢你的回答Daniel,但是直方图仍然是默认的蓝色,即使我尝试将绘图输出赋值给一个对象并使用suppressWarnings()函数。发布的超链接与在plotly上更改直方图颜色无关,而是与隐藏警告信息的最佳方法有关。 - Stefano Leone
1个回答

3
以下的绘图是否有效?必须为marker属性指定color参数。
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"))

enter image description here


非常感谢Ameya,我在plotly上找到了许多关于其他图形的详细信息,但不幸的是我错过了直方图的这些信息。现在它可以正常工作了,再次感谢! - Stefano Leone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接