在双轴图上以不同的水平绘制zerolines

时间:2017-10-25 15:19:49

标签: r graph data-visualization plotly

我试图在图表中叠加折线图和条形图(用垂直线指定一个重要的日期),我遇到了这个问题,其中两条零线偏移而不是在同一条线上。我已尝试使用overlaying = 'y'中的layout选项,并尝试更改三个trace组件的顺序,但似乎没有任何帮助。任何想法如何解决?下面是我的虚拟数据代码:

(另外,如果你可以解决我的传奇 - 重叠-y2轴问题,可以获得奖励积分)

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)
     )

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将rangemode='nonnegative'添加到# open the file for appending (the "a"). Create it if it doesn't exist (the "+") with open(filename, "a+") as f: w = csv.writer(f, delimiter=",") # only write the headers if the file is blank (i.e. the first time the program runs) if f.read() == "": w.writerows[headers] # get `details` from the user. # ... # the data always gets appended w.writerows(details) 或通过layout手动指定range

对于您的红利问题,您可以设置图例的x-position,例如 range=list(0, max(wires_mnth)

enter image description here

相关问题