简体   繁体   English

使用 R plotly 将图案或阴影添加到条形图

[英]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.我在 R 中创建条形图,并希望根据特定(二进制)特征的值添加特定图案或阴影。 Does anyone know of a way to do this in plotly?有谁知道在 plotly 中执行此操作的方法? I've seen it done in ggplot and in python, but not in this case.我已经在 ggplot 和 python 中看到过它,但在这种情况下没有。

Edit: Say the data is plot_data编辑:说数据是 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 的代码是:

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.因此有 5 个不同高度的条具有两个不同的 colors,具体取决于它们是 name_1 还是 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.我还想让它们有一个取决于 pattern_var 是 1 还是 0 的模式,这样如果 pattern_var 为 0,则条形完全被颜色填充,如果 pattern_var 为 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.如果我确切地知道您想要什么会更好,但这可以向您展示 function 调用中如何记录模式。

library(plotly)

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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