简体   繁体   中英

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.

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")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM