如何在绘图中的标题周围添加填充?

goo*_*lan 2 r ggplot2 plotly

我正在将我的ggplot2图表转换为情节。我想手动添加标题,因为我想留出空间以便之后添加副标题 - 这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) +
  # geom_text_repel() +
  labs(
    # title = "Purchasing Retention Analysis",
    subtitle = "Customers who order at least one item each week",
    y = "Ratio of Customers",
    x = "Relative Month"
  ) + theme_light()
Run Code Online (Sandbox Code Playgroud)

文档说我可以使用 pad 设置填充,但是当我将其添加到函数中时,它会出现错误,指出它是无效参数layout

ggplotly(g) %>%    
  layout(
    title = "My Title",
    pad = list(b = 90, l = 130, r = 50 ))
Run Code Online (Sandbox Code Playgroud)

Nar*_*ali 8

标题应如下所示创建。

library(plotly)
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")%>%    
  layout(title = list(text = "My Title",pad = list(b = 90, l = 130, r = 50 )))
p
Run Code Online (Sandbox Code Playgroud)

其中是一个包含值和 的title列表。如果这能解决您的问题,请告诉我!textpad