【发布时间】: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 直方图的颜色