Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly - How to change the Histogram colour?

I am trying to plot a histogram with a specific colour since the purpose is to do create two histograms from two different data frames and I do not want to present both of them with the default blue colour. I am aware that a solution would imply the conversion of the ggplo2 object into plotly, but I would like to find out a way to solve this small problem in the plotly code. The code for a basic plotly histogram is the following:

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

The two solutions I have tried do not work:

1) First attempt:

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

It returns the following warning message:

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

2) Second attempt:

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

It returns the following warning message:

    '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'

Any suggestions?

like image 769
Stefano Leone Avatar asked Oct 17 '22 15:10

Stefano Leone


1 Answers

Does the plot below work? The color argument must be specified to the marker attribute.

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

like image 128
Ameya Avatar answered Oct 20 '22 05:10

Ameya