Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep my subtitles when I use ggplotly()

Tags:

r

ggplot2

plotly

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?

like image 638
goollan Avatar asked Apr 30 '19 14:04

goollan


1 Answers

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>')))
like image 101
Melissa Salazar Avatar answered Nov 04 '22 21:11

Melissa Salazar