使用ggplotly()时如何保留字幕 [英] How do I keep my subtitles when I use ggplotly()

查看:93
本文介绍了使用ggplotly()时如何保留字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这张图表.

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实体后,是否有办法强制其保留字幕或添加新文本作为字幕?

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?

推荐答案

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

This helped me. Seems any text such as annotations or subtitles added to ggplot must be put after the ggplotly function into a layout function. 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>')))

这篇关于使用ggplotly()时如何保留字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆