13

I have this chart.

g <- retention_cohorts %>% 
  ggplot(aes(relative_week, round(percent,2), label=relative_week)) + 
  geom_line() +
  scale_y_continuous(labels = percent) +
  scale_x_continuous(breaks = seq(1,24,4),
                     labels = seq(1,6)
  ) +
  geom_text(nudge_y = .02) +
  labs(
    title = "Purchasing Retention Analysis",
    subtitle = "Customers who order at least one item each week",
    y = "Customers with at least One Purchase",
    x = "Relative Month"
  ) + theme_light()

It's a great chart and I want to make it interactive. When I use ggplotly(g), it recreates it almost perfectly except that it drops the subtitle. Is there a way to force it to keep the subtitle or to add new text as a subtitles after the plotly entity has been created?

goollan
  • 665
  • 7
  • 16
  • 2
    There's an open issue to add this functionality. It looks like adding the text afterwards is the likely solution https://github.com/ropensci/plotly/issues/799 – goollan Apr 30 '19 at 14:37

1 Answers1

14

This helped me, Subtitles with ggplotly. Seems any text such as annotations or subtitles added to ggplot must be put after the ggplotly function into a layout function.

plot <- retention_cohorts %>% 
  ggplot(aes(relative_week, round(percent,2), label=relative_week)) + 
  geom_line() +
  scale_y_continuous(labels = percent) +
  scale_x_continuous(breaks = seq(1,24,4),
                     labels = seq(1,6)) +
  geom_text(nudge_y = .02) +
  labs(y = "Customers with at least One Purchase",
       x = "Relative Month") + 
  theme_light()

ggplotly(plot) %>%
  layout(title = list(text = paste0('Purchasing Retention Analysis',
                                    '<br>',
                                    '<sup>',
                                     'Customers who order at least one item each week','</sup>')))
Melissa Salazar
  • 416
  • 5
  • 15