使用ggplotly()时如何保留字幕

时间:2019-04-30 14:35:36

标签: r ggplot2 plotly

我有这张图表。

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()

这是一张很棒的图表,我想使其互动。当我使用ggplotly(g)时,它会完美地重新创建它,只是会删除字幕。在创建plotly实体之后,是否有办法强迫它保留字幕或添加新文本作为字幕?

1 个答案:

答案 0 :(得分:3)

这帮助了我。似乎添加到ggplot中的任何文本(例如注释或字幕)都必须放在ggplotly函数之后,放入布局函数中。 https://datascott.com/blog/subtitles-with-ggplotly/

ggplotly(p) %>%
  layout(title = list(text = paste0('US State Population and Life Expectancy',
                                    '<br>',
                                    '<sup>',
                                    'Life expectancy 1969-1971; Population estimate as of July 1, 1975',
                                    '</sup>')))