使用r绘图将图案或舱口添加到条形图
我正在在R中创建条形图,并希望根据特定(二进制)功能的值添加特定模式或舱口。有人知道在情节中做到这一点的方法吗?我已经在GGPLOT和PYTHON中看到了它,但在这种情况下还没有。
编辑:说数据是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
现在我的制作绘图的代码是:
plot_ly(data = plot_data) %>%
add_bars(
x = ~x_var,
y = ~y_var,
color = ~color_var,
)
这样的高度有5个不同的颜色,这些颜色具有两种不同的颜色,具体取决于它们是name_1还是name_2。我还要让它们具有模式,具体取决于pattern_var是1还是0,因此,如果pattern_var为0,则栏完全用颜色填充,如果tatter_var为1,则具有条纹或其他一些模式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我确切地知道您想要的东西,那就更好了,但是这可以向您展示如何在函数调用中记录模式。
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.
这是一个示例,将
pattern_var
与模式“/”:=“ https://i.sstatic.net/xabxn.png” alt =“ plotly with strupted strupterty”>
您还可以修改plot_data data.frame以准确定义pattern_var列中所需的模式(“+”+ “,”。“,”/“,” - “等等
) nofollow noreferrer”> https://plotly.com/r/reference/bar/#bar-marker-pattern-shape
编辑:
由于{plotly}中的错误,上述代码无法正常工作(形状(形状)未分配给正确的轨迹)。
解决方法是对数据进行排序。帧颜色以使其有效:
可以在此处找到对情节问题的引用:
https://github.com/plotly/plotly.r/issues/1744
https://github.com/plotly/plotly.r/issues/1155
Here is an example that matches
pattern_var
to the pattern "/":You can also modify the plot_data data.frame to define exactly the pattern needed in pattern_var column ("+", ".", "/", "-", etc...)
https://plotly.com/r/reference/bar/#bar-marker-pattern-shape
EDIT:
Due to a bug in {plotly}, the above code was not working correctly (shapes are not assigned to the correct traces).
The workaround is to sort the data.frame by color to make it work:
References to plotly issue can be found here:
https://github.com/plotly/plotly.R/issues/1744
https://github.com/plotly/plotly.R/issues/1155