使用r绘图将图案或舱口添加到条形图

发布于 2025-01-21 17:41:47 字数 617 浏览 2 评论 0原文

我正在在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小猫一只 2025-01-28 17:41:47

如果我确切地知道您想要的东西,那就更好了,但是这可以向您展示如何在函数调用中记录模式。

library(plotly)

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

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

彻夜缠绵 2025-01-28 17:41:47

这是一个示例,将pattern_var与模式“/”:

library(plotly)
plot_data <- data.frame(
  x_var = 1:5,
  y_var = c(4, 3, 6, 3, 7),
  color_var = c("name_1", "name_2", "name_1", "name_1", "name_2"),
  pattern_var = c(1, 0, 0, 1, 1)
)


plot_ly(data = plot_data) |> 
  add_bars(
    x = ~x_var,
    y = ~y_var,
    color = ~color_var,
    marker = list(
      pattern = list(
        shape = ~ifelse(pattern_var == 1, "/", "")
      )
    )
  )

=“ 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}中的错误,上述代码无法正常工作(形状(形状)未分配给正确的轨迹)。

解决方法是对数据进行排序。帧颜色以使其有效:

    plot_data[order(plot_data$color_var),] |>
      plot_ly() |> 
      add_bars(
        x = ~x_var,
        y = ~y_var,
        color = ~color_var,
        marker = list(
          pattern = list(
            shape = ~ifelse(pattern_var == 1, "/", "")
          )
        )
      )

“具有正确形状的图”

可以在此处找到对情节问题的引用:

https://github.com/plotly/plotly.r/issues/1744

https://github.com/plotly/plotly.r/issues/1155

Here is an example that matches pattern_varto the pattern "/":

library(plotly)
plot_data <- data.frame(
  x_var = 1:5,
  y_var = c(4, 3, 6, 3, 7),
  color_var = c("name_1", "name_2", "name_1", "name_1", "name_2"),
  pattern_var = c(1, 0, 0, 1, 1)
)


plot_ly(data = plot_data) |> 
  add_bars(
    x = ~x_var,
    y = ~y_var,
    color = ~color_var,
    marker = list(
      pattern = list(
        shape = ~ifelse(pattern_var == 1, "/", "")
      )
    )
  )

plotly with stripped 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:

    plot_data[order(plot_data$color_var),] |>
      plot_ly() |> 
      add_bars(
        x = ~x_var,
        y = ~y_var,
        color = ~color_var,
        marker = list(
          pattern = list(
            shape = ~ifelse(pattern_var == 1, "/", "")
          )
        )
      )

plot with correct shapes

References to plotly issue can be found here:

https://github.com/plotly/plotly.R/issues/1744

https://github.com/plotly/plotly.R/issues/1155

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文