'Adding pattern or hatch to bar graph using R plotly

I'm creating bar graphs in R, and want to add a specific pattern or hatch depending on the value of a specific (binary) feature. Does anyone know of a way to do this in plotly? I've seen it done in ggplot and in python, but not in this case.

Edit: Say the data is plot_data

x_var y_var color_var pattern_var
1     4     name_1    1
2     3     name_2    0
3     6     name_1    0
4     3     name_1    1
5     7     name_2    1

Right now my code for making the plot is:

plot_ly(data = plot_data) %>%
    add_bars(
        x = ~x_var,
        y = ~y_var,
        color = ~color_var,
    )

so that there are 5 bars of different heights that have two different colors depending on whether they are name_1 or name_2. I want to also have them have a pattern depending on whether pattern_var is 1 or 0, so that if pattern_var is 0, the bar is filled in completely by the color, and if pattern_var is 1, it has stripes or some other pattern.



Solution 1:[1]

It would be better if I knew exactly what you wanted, but this works to show you how patterns are documented in the function call.

library(plotly)

plot_ly(x = LETTERS[1:3],
        y = 6:4,
        color = letters[8:10],
        marker = list(pattern = list(shape = "x")),
        type = "bar")

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kat