plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.55k stars 622 forks source link

geom_flow() has yet to be implemented in plotly #2227

Open jonathanlee1993 opened 1 year ago

jonathanlee1993 commented 1 year ago

I've added a reproducible example here. The plot will work under normal ggplot conditions but not if you send it to ggplotly

library(ggplot2)
library(ggalluvial)
library(tidyverse)

data(vaccinations)
levels(vaccinations$response) <- rev(levels(vaccinations$response))

vaccinations <- vaccinations %>% 
    group_by(survey) %>% 
    mutate(pct = freq / sum(freq)) %>% 
    ungroup() %>% 
    mutate(
        test_color = case_when(
            subject %in% {vaccinations %>% 
                filter(
                    survey == "ms460_NSA" & response %in% "Never"
                ) %>% 
                distinct(subject) %>% 
                pull()} ~ "Yes",
            TRUE        ~ "No"
        )
    )

g <- ggplot(vaccinations,
       aes(x = survey, stratum = response, alluvium = subject,
           y = pct,
           fill = test_color,
           label = response)) +
    scale_x_discrete(expand = c(.1, .1)) +
    scale_y_continuous(label = scales::percent_format()) +
    scale_fill_manual(values = c("Yes" = "cadetblue1", "No" = "grey50")) +
    geom_flow() +
    geom_stratum(alpha = .5) +
    geom_text(aes(label = paste0(..stratum.., "\n", scales::percent(..count.., accuracy = .1))), stat = "stratum", size = 3) +
    theme(legend.position = "none") +
    ggtitle("vaccination survey responses at three points in time")

g
g %>% ggplotly()
arcenis-r commented 3 months ago

I also experienced the same issue as @jonathanlee1993. I noticed that the strata are represented, but the flow between the strata is not visually represented when run as a ggplotly() object.