在双轴图上绘制不同级别的零线

rmb*_*man 5 r data-visualization graph plotly

我试图以绘图方式覆盖折线图和条形图(用一条垂直线指定重要日期),但我遇到了这个问题,其中两条零线偏移而不是在同一条线上。我尝试过摆弄overlaying = 'y'其中的选项layout,并尝试更改三个组件的顺序trace,但似乎没有任何帮助。有什么想法如何修复吗?下面是我的带有虚拟数据的代码:

(此外,如果您能解决我的图例重叠 y2axis 问题,还可获得奖励积分)

date <- seq(as.Date("2015/6/1"), by = "month", length.out = 19) 
wires_mnth <- c(rep(0,8),100000,750000,1200000,2500000,3100000,5500000,7500000,8000000,9900000,11300000,11000000)
wires_cnt <- c(rep(0,8),100,200,250,325,475,600,750,800,1000,1150,1200)
data <- data.frame(date, wires_mnth)

plot_ly(data) %>%
add_trace(x = ~date, y = ~wires_cnt, type = 'bar', name = 'Wires Count', 
    yaxis = 'y2', opacity = .5) %>%
add_trace(x = ~date, y = ~wires_mnth, type = 'scatter', mode = 'lines', name 
    = 'Monthly Wires') %>%
add_trace(x = c(2016,2016), y = c(0, 12000000), type = 'scatter', mode = 
    "lines", name = 'Sanctions Removed') %>%

layout(title = 'Monthly Aggregate Wire Transfers to Iran',
     xaxis = list(title = ''),
     yaxis = list(side = 'left', title = 'Wire Amounts (USD)', showgrid = 
         FALSE, zeroline = FALSE),
     yaxis2 = list(side = 'right', overlaying = 'y', title = 'Wires Count', 
        showgrid = FALSE, zeroline = FALSE)
     )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Max*_*ers 5

您可以添加rangemode='nonnegative'到您的或通过手动layout指定。rangerange=list(0, max(wires_mnth)

对于您的奖励问题,您可以设置图例的 x 位置,例如legend = list(x = 1.2)

在此输入图像描述