I'm trying to overlay a barplot with a scatter plot in plotly. I've managed to plot both and everything looks nice using only markers, but I need to join the markers. Using lines+markers
it joins every point with each other, like a closed path. I suppose the problem is that my x-axis variable is categorical and I don't know how to fix it. I can't write the original code but is something like:
to_plot2[, cat_var := as.factor(cat_var)]
pp2 = plot_ly(to_plot2) %>%
add_trace(x = ~cat_var, y = ~var_1, type = 'bar', name = 'bar',
marker = list(color = '#C9EFF9'),
hoverinfo = "text") %>%
add_trace(x = ~cat_var, y = ~var_2, type = 'scatter', mode = 'lines+markers', name = 'lines', yaxis = 'y2',
hoverinfo = "text") %>%
layout(title = 'foo',
xaxis = list(title = "", type = "category",
categoryorder = "'array'"),
yaxis = list(side = 'left', showgrid = FALSE, zeroline = FALSE),
yaxis2 = list(side = 'right', overlaying = "y", showgrid = FALSE, zeroline = FALSE))
and the output is:
Any help?
Thanks in advance
答案 0 :(得分:0)
我刚刚解决了这个问题。将cat_var
设置为关键字,它会以适当的方式重新排序数据框。
谢谢!