Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add bar outline to an R plotly bar chart?

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading