disp_black_rr <- disp_black %>%
dplyr::select(rr, ci_low, ci_upp, p, warning) %>%
dplyr::mutate(
ethnicity = "black", .before = rr
)
disp_asian_rr <- disp_asian %>%
dplyr::select(rr, ci_low, ci_upp, p, warning) %>%
dplyr::mutate(
ethnicity = "asian", .before = rr
)
disp_mixed_rr <- disp_mixed %>%
dplyr::select(rr, ci_low, ci_upp, p, warning) %>%
dplyr::mutate(
ethnicity = "mixed", .before = rr
)
disp_other_rr <- disp_other %>%
dplyr::select(rr, ci_low, ci_upp, p, warning) %>%
dplyr::mutate(
ethnicity = "other", .before = rr
)
disp_all <- rbind(disp_black_rr,
disp_asian_rr,
disp_mixed_rr,
disp_other_rr)
disp_all <- disp_all %>%
mutate(
p_clean = case_when(p < .001 ~ "< .001",
TRUE ~ as.character(paste0("= ", p)))
)
library(forcats)
library(dplyr)
library(plotly)
# make sure ordering is correct (like fct_reorder in ggplot)
disp_all <- disp_all %>%
mutate(
ethnicity = stringr::str_to_title(ethnicity),
ethnicity = forcats::fct_reorder(ethnicity, rr)
)
plot_ly(
data = disp_all,
x = ~rr,
y = ~ethnicity,
type = "scatter",
mode = "markers",
marker = list(size = 8, color = "black"),
error_x = ~list(
type = "data",
symmetric = FALSE,
array = ci_upp - rr, # upper error
arrayminus = rr - ci_low, # lower error
thickness = 1.5,
width = 5,
color = jk_colours_primary[3]
),
text = ~paste0(
"<b>", ethnicity, "</b><br>",
"RR: ", round(rr, 2), "<br>",
"95% CI: [", round(ci_low, 2), ", ", round(ci_upp, 2), "]"
),
hoverinfo = "text"
) %>%
# add labels (like geom_text)
# add_text(
# x = ~rr + 0.2,
# y = ~ethnicity,
# text = ~round(rr, 2),
# textfont = list(size = 14),
# showlegend = FALSE
# ) %>%
layout(
xaxis = list(
title = "Relative risk ratio",
range = c(0, 3.5),
tickvals = seq(0, 3, 0.5)
),
yaxis = list(
title = "",
categoryorder = "array",
categoryarray = levels(disp_all$ethnicity)
),
shapes = list(
list( # vertical red dashed line
type = "line",
x0 = 1, x1 = 1,
y0 = -0.5, y1 = length(unique(disp_all$ethnicity)) + 0.5,
line = list(color = "red", dash = "dash")
)
),
annotations = list(
list(
x = 1.1,
y = max(as.numeric(disp_all$ethnicity)) - 0.5, # near the top
text = "more likely to be stopped →<br>than White people",
showarrow = FALSE,
xanchor = "left", # like hjust
yanchor = "bottom", # like vjust
font = list(size = 14, color = "gray50")
),
list(
x = 0.95,
y = max(as.numeric(disp_all$ethnicity)) - 0.5, # near the top
text = "\u2190 less likely to be stopped<br>than White people",
showarrow = FALSE,
xanchor = "right", # like hjust
yanchor = "bottom", # like vjust
font = list(size = 14, color = "gray50")
)
)
) |>
my_plotly()