[英]Place Plotly Bar Chart and Box Plot in Front of Line Traces
我在Plotly
中创建了子图,每个子图包含一个条形图(或boxplot
)和三个跟踪线。 我在y = 1,2,3处创建了迹线,像ablines
一样充当ggplot
。
和
。
我想要它,因此条形图的条形位于跟踪线的前面 ,因此您应该只能看到条形之间的跟踪线。
(我排除了生成子图的代码,因为我认为这不是必需的)
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))
我认为创建跟踪的顺序将定义它们在图形中的哪一层,因此由于我在所有跟踪线之后绘制了条形图,因此它位于它们的上方。
我也尝试过将形状制作为ablines
但要使其正确地放置在子图上确实很困难。 add_traces
是我最好的方法。 图中的形状具有layer
参数,以定义是将形状放置在上方还是下方( 请参见图参考 )。 我希望有类似的东西适用于痕迹,但我找不到它。
也许这可以帮助您开始。 您没有提供数据集,因此这里是一个小例子。 您可以使用布局为图形添加形状(在您的情况下为线)。 如您所见,条形图位于线条上方。 希望能帮助到你!
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.