Home > Software engineering >  How to add bar outline to an R plotly bar chart?
How to add bar outline to an R plotly bar chart?

Time:05-17

I am creating a plotly chart with individual bars coded based on a variable category. I am having trouble adding a black outline to the bars while keeping the bars' color categorization.

Below is my current code. What do I need to add to include a black outline on each bar? Thank you.

library(plotly)
library(tidyverse) 

my_tibble <- tibble(mins = runif(10,10,30),
                     week = 1:10,
                     exercise = c("a", "b", "b", "b", "a", "a", "b", "b", "b", "a"))

example_hex <- c('#70AD47', '#404040', '#CAE1F1', '#24608B')

plot_ly(
  data = my_tibble,
  type = 'bar',
  x = ~week,
  y = ~mins,
  color = ~exercise,
  colors = example_hex)

CodePudding user response:

The marker parameter is what you are after.

library(plotly)
library(tidyverse) 

my_tibble <- tibble(mins = runif(10,10,30),
                    week = 1:10,
                    exercise = c("a", "b", "b", "b", "a", "a", "b", "b", "b", "a"))

example_hex <- c('#70AD47', '#404040', '#CAE1F1', '#24608B')

plot_ly(data = my_tibble) %>% 
  add_trace(
    type = 'bar',
    x = ~week,
    y = ~mins,
    color = ~exercise,
    marker = list(line = list(color = "black", width = 5)))

Output:

enter image description here

  • Related