简体   繁体   English

将条形图和箱形图放在线迹的前面

[英]Place Plotly Bar Chart and Box Plot in Front of Line Traces

I have created subplots in Plotly that each contain a bar chart (or boxplot ) and three trace lines. 我在Plotly中创建了子图,每个子图包含一个条形图(或boxplot )和三个跟踪线。 I have created traces at y= 1,2,3 to act as ablines like in ggplot . 我在y = 1,2,3处创建了迹线,像ablines一样充当ggplot

What the plots look like: 情节是什么样的:

图片链接到情节的图片

and

子图的图片 .

Problem: 问题:

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. 我想要它,因此条形图的条形位于跟踪线的前面 ,因此您应该只能看到条形之间的跟踪线。

My code currently: 我的代码当前:

(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. 我认为创建跟踪的顺序将定义它们在图形中的哪一层,因此由于我在所有跟踪线之后绘制了条形图,因此它位于它们的上方。

I also tried creating shapes to be the ablines but it was really difficult to get them in the correct position for subplots. 我也尝试过将形状制作为ablines但要使其正确地放置在子图上确实很困难。 add_traces was my best approach. add_traces是我最好的方法。 Shapes in plotly have a layer parameter to define whether to place the shape above or below ( see the plotly reference ). 图中的形状具有layer参数,以定义是将形状放置在上方还是下方( 请参见图参考 )。 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")))

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

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