0

I am trying to customise my horizontal bar chart, in plotly R, in such a way that the top 5 bars (which represent highest 5 values out of the total 20) are colored differently, say Green, and the rest 15 having default blue colors. How do I go about achieving that? I appreciate any help. For the sake of reproducing an example, assume that my data frame is as follows:

categories<- letters[1:20]
values <- sort(runif(20, 1, 40), decreasing = TRUE)
df <- data.frame(categories, values)

And the standard plotly horizontal bar chart (which I am looking to customise its colours) was created using the following code:

# To make horizontal bars appear in decreasing order, set factor level order to ascending

  df$categories <- factor(df$categories, levels = unique(
                              df$categories)[order(df$values,
                                                     decreasing = FALSE)]

# Plot the poorly bar chart using poorly
                            )
  fig <- plot_ly(df, x = ~values, y = ~categories, 
          type = 'bar', orientation = 'h')

A screenshot of the standard bar chart the above codes produced is given below:

enter image description here

Now, the question is how do I convert the colors of the first 5 bars (for bars a, b, c, d, and e) to green, leaving the rest as they are in blue color.

0 Answers0