I would like to try to generate a similar plot like this using plot_ly (instead of directly using ggplotly)
ggplot
library(tidyverse)
# using ggplot2 can get the results
df <- data.frame(
trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
upper = c(1.1, 5.3, 3.3, 4.2),
lower = c(0.8, 4.6, 2.4, 3.6)
)
p <- ggplot(df, aes(trt, resp, colour = group))
p + geom_linerange(aes(ymin = lower, ymax = upper), position = position_dodge(width = 0.5))
plotly
For plotly, the most similar I can get is like this. Is there a way to dodge the lines?
Thank you!
# using plot_ly
library(plotly)
library(tidyverse)
df <- data.frame(
trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
upper = c(1.1, 5.3, 3.3, 4.2),
lower = c(0.8, 4.6, 2.4, 3.6)
)
df1 <- df %>% pivot_longer(names_to = "po", cols = c("resp", "upper", "lower"))
df1 %>% group_by(trt) %>% plot_ly( x = ~trt, y = ~value, color = ~group) %>% add_trace(type = "scatter", mode = "markers+lines")

