2

I am creating a simple Plotly bar plot with following data, where the color of the individual Bars will be varying based on the respective values of the Bar. However range of the color should be defined i.e. ranging from Blue (smallest) to Red (highest)

df <- data.frame(x = c('a', 'B'), 
                 y = c(100, 50),
                 z = c(100, 50))


plot_ly(data = df, 
        x = ~x, 
        y = ~y, 
        marker = list(color = ~z), 
        colors = c("blue", "red"), 
        type = "bar")

Unfortunately Plotly displays Bar chart with something different Color-scheme. Could you please point me the right code .

Thanks in advance

1 Answer 1

1

I guess you want something like this:

plot_ly(data = df, 
            x = ~x, 
            y = ~y, 
            marker = list(color =c("blue", "red")), 
            type = "bar")

[EDIT]:

As per the comments I have edited the code. Hope this is what you want.

 df <- data.frame(x = c('a', 'B', "c", "d", "e"), 
                     y = c(100, 50, 20, 10, 100),
                     z = c(100, 50,  20, 10, 100))


    colfunc <- colorRampPalette(c("blue", "red"))
    pal <- colfunc(max(df$z))[df$z]

    plot_ly(data = df, 
            x = ~x, 
            y = ~y, 
            marker = list(color =  pal), 
            type = "bar")
Sign up to request clarification or add additional context in comments.

1 Comment

not really, Bar color to be picked from a pallet ranging from Blue to Red, and exact color will be based on value/height of the Bar. Means, if my Bar's height is very small then it will be closer to Blue, and high-height will be closer to Red. Please note : the data 'df' is not a static data.frame, it is calculated/derived based on some upstream calculation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.