question_data <- data.frame(question = c("q1", "q1", "q1", "q1", "q1", "q1",
"q2", "q2", "q2", "q2", "q2", "q2"),
option = c("Y", "M", "N", "Y", "M", "N",
"Y", "M", "N", "Y", "M", "N"),
values = c(1, 0.6, 0.3, 1, 0.8, 0.1,
1, 0.7, 0.2, 1, 0.5, 0.4),
breakdown = c("M", "M", "M", "F", "F", "F",
"M", "M", "M", "F", "F", "F"))
p <- question_data %>%
ggplot(aes(x = breakdown, y = values, fill = option, tooltip = paste0(question, ":", option),
alpha = question, data_id = question)) +
geom_col_interactive(aes(`data-id` = question), position = "identity",
extra_interactive_params = "data-id") +
scale_alpha_manual_interactive(extra_interactive_params = "data-id",
values = c(0, 0),
`data-id` = unique(question_data$question),
data_id = function(breaks) as.character(breaks)) +
scale_fill_phic() +
theme_phic()
default_series <- "q1"
p2 <- girafe(ggobj = p, options = list(
opts_selection(girafe_css("fill-opacity:1;"), type = "single",
selected = default_series, only_shiny = FALSE),
opts_hover(girafe_css("fill-opacity:1;")), # stop series disappear on hover
opts_selection_key(girafe_css(paste0("fill:", "#3d5073", "!important ;"))),
opts_hover_key(girafe_css(paste0("fill:", "#b8d8e2",
"; fill-opacity:1 !important"))), # make key not orange on hover
opts_selection_inv(girafe_css("pointer-events: none !important; fill-opacity:0 !important;")))) # ensure right tooltip shows
# in ggiraph objects, the g element with a clip-path ending in 'c1)' is the legend - find it and enable pointer events
prependContent(p2, htmltools::tags$style(".ggiraph-svg-rootg g[clip-path$='c1)'] rect[class^='select_'] { pointer-events: all !important;}"))