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

标签 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 之后添加新文本作为副标题?实体已创建?

最佳答案

这对我有帮助,Subtitles with ggplotly .似乎任何添加到 ggplot 的注释或字幕等文本都必须在 ggplotly 函数之后放入布局函数中。

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

关于r - 使用 ggplotly() 时如何保留字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55923256/

相关文章:

在 R 中从 Weka 的决策树运行统计测试

r - 使用facet_wrap()在ggplot2直方图中相等的条形宽度

删除包含 3 个或更少点的数据的回归线

python - 文本位置未显示在绘图上

python - Plotly:悬停文本字段中的日期格式问题

python - Plotly 在 jupyter lab 中给出一个空字段作为输出

r - 匹配数据框中列的文本

基于列组的列随机抽样

r - 如何将变量重新编码为数字?

r - 更改 ggplot 中图例形状的填充