将绘图条形图和箱线图放在线迹前面 [英] Place Plotly Bar Chart and Box Plot in Front of Line Traces

查看:57
本文介绍了将绘图条形图和箱线图放在线迹前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Plotly 中创建了子图,每个子图都包含一个条形图(或 boxplot)和三个跟踪线.我在 y= 1,2,3 处创建了跟踪作为 ablines 就像在 ggplot 中一样.

I have created subplots in Plotly that each contain a bar chart (or boxplot) and three trace lines. I have created traces at y= 1,2,3 to act as ablines like in ggplot.

.

我想让条形图的条形在跟踪线的前面,所以您应该只能看到条形之间的跟踪线.

I want to have it so the bars of the bar chart are in front of the trace lines so you should only be able to see the trace lines in between the bars.

(我已经排除了生成子图的代码,因为我认为不需要)

(I have excluded the code that generates the subplots as I don't think it is needed)

  generate_plotly_barPlot <- function(dat, showLeg, err) {

    p <- plot_ly(x=dat$xVar, # Initialize graph with line y=2
                 y=2,
                 type="scatter",
                 mode="lines",
                 hoverinfo="none",
                 layer="below",
                 line=list(color="grey",
                           width=2),
                 showlegend=FALSE) %>%
      # Add trace at line y=1
      add_trace(x=dat$xVar,
                y=1,
                type="scatter",
                mode="lines",
                hoverinfo="none",
                line=list(color="grey",
                          width=1.5,
                          dash="dot"),
                inherit=FALSE,
                showlegend=FALSE) %>%
      # Add trace at line y=3
      add_trace(x=dat$xVar,
                y=3,
                type="scatter",
                mode="lines",
                hoverinfo="none",
                line=list(color="grey",
                          width=1.5,
                          dash="dot"),
                inherit=FALSE,
                showlegend=FALSE)%>%
       # Create Bar Chart 
       add_trace(x=dat$xVar, 
                 y=dat$CopyNumber, 
                 type="bar",
                 mode="markers",
                 color=dat$fillColor,
                 orientation="v",
                 inherit=FALSE,
                 marker=list(opacity=1),
                 legendgroup=dat$xVar,
                 showlegend=showLeg,
                 error_y = list(value=dat[[err]],
                                color='#000000',
                                thickness=3,
                                width=6,
                                visible=TRUE))

我的方法:

我认为创建轨迹的顺序将定义它们将在图表中的哪个层,因此因为我在所有轨迹线之后绘制了条形图,所以它会位于它们之上.

My approach:

I thought that the order the traces were created would define which layer they would be on in the graph, so since I plotted the bar chart after all of the trace lines, it would sit above them.

我也尝试创建形状作为 ablines,但很难让它们处于子图的正确位置.add_traces 是我最好的方法.plotly 中的形状有一个 layer 参数来定义是将形状放在上面还是下面(参见情节参考).我希望有类似的东西适用于痕迹,但我找不到.

I also tried creating shapes to be the ablinesbut it was really difficult to get them in the correct position for subplots. add_traces was my best approach. Shapes in plotly have a layer parameter to define whether to place the shape above or below (see the plotly reference). I was hoping there was something like this that applied to traces, but I couldn't find it.

推荐答案

也许这可以帮助您开始.您没有提供数据集,所以这里是一个小的虚构示例.您可以使用布局向图形添加形状(在您的案例中为一条线).如您所见,该条位于线上方.希望能帮到你!

Maybe this could help you started. You did not provide a data set so here is a small made-up example. You can use layout to add a shape (a line in your case) to your graphic. As you can see the bar is above the line. Hope it helps!

plot_ly() %>% 
add_bars(x = c("giraffes", "orangutans", "monkeys"), y = c(20, 14, 23)) %>% 
layout(shapes = list(list(type = "lines",x0 = -1, x1 = 3, xref = "x", y0 = 3, y1 = 3, yref = "y", layer = "below")))

这篇关于将绘图条形图和箱线图放在线迹前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆