When creating a bar plot in plotly, it automatically offsets bars next to one another, but if I add points to the same plot, the points are not offset. How do I offset the points to match the bars?
library(dplyr)
plotly::plot_ly() %>%
plotly::add_trace(data = iris %>%
dplyr::group_by(Species) %>%
dplyr::summarize(y1 = mean(Sepal.Length),
y2 = mean(Sepal.Width)) %>%
tidyr::pivot_longer(cols = c('y1', 'y2')),
x = ~Species,
y = ~value,
type = 'bar',
name = ~name,
color = ~name,
legendgroup = ~name,
# only show a single legend
showlegend = TRUE) %>%
plotly::add_trace(
data = iris %>%
dplyr::group_by(Species) %>%
dplyr::summarize(y1 = mean(Sepal.Length),
y2 = mean(Sepal.Width)) %>%
dplyr::filter(y1 > 6) %>%
tidyr::pivot_longer(cols = c('y1', 'y2')),
x = ~Species,
y = ~value,
type = 'scatter',
mode = 'markers',
name = ~name,
color = ~name,
legendgroup = ~name,
marker = list(size = 10)
)
This makes me think that it is possible, but I don’t see how to do it.