ggplotly- 绘图标题与图形重叠

Posted

技术标签:

【中文标题】ggplotly- 绘图标题与图形重叠【英文标题】:ggplotly- Plot title overlapping with graph 【发布时间】:2020-10-30 20:16:43 【问题描述】:

我正在 Rstudio 中绘制 facet_grid 图,显示每日趋势图。当我使用 ggplotly 时,绘图标题与主图重叠。

names(ss)[1] <- "State_OR_UT"
  state_tr1 <- 'Maharashtra'
  state_tr2 <- 'Delhi' 
  plot_sales_hp <- ggplot(ss, aes(x= Date,y = ValuesTotals,group=1))+
    geom_line(aes(color = State_OR_UT)) + 
    facet_grid(.~TypeOfSales)+
    
    labs(title =  paste0("Trend Graph between ",state_tr1," and ",state_tr2," <br />\n till ",
                         strftime(max(ss$Date), format = "%b/%d/%Y")),
         x = "Date", y = "Number of Sales")+
    theme(
      plot.title = element_text(size = 20,hjust=0.5, face = "bold", color = "darkgreen"),
      axis.text.y=element_blank() )
  
  
  plotly_sales <- ggplotly(plot_sales_hp,tooltip = "text")%>% layout(margin = list(l = 75))
  print(plotly_sales)

标题在图表上重叠。 我尝试过的事情:

    根据Text wrap for plot titles 实验室参数的变化:

      paste0(strwrap("Trend Graph between ",state_tr1," and ",state_tr2," <br />\n till ",strftime(max(ss$Date), format = "%b/%d/%Y"),width = 20),collapse = "\n")
    

    在主题中调整 vjust - 标题仍然在图表上重叠

    根据Fix plotly ggplotly() Title Overlapping Plot When Title is Split on Two Lines 我在实验室中添加了 \n - 标题参数但没有用

    根据R: ggplot and plotly axis margin won't change 我试图在下面找到标题,以便我可以调整它。还是没有运气

     str(plotly_sales[['x']][['layout']][['annotations']])
    

我在 Shiny R 的 renderPlotly 中使用这个输出。 当名称很长时,是否有任何其他方式可以自动调整绘图和图形的标题,例如 ggplotly 中的 (Andaman and Nicobar Island) vs (Dadra and Nagar Haveli and Daman and Diu)

注意:我已经在我的原始代码中格式化了 xaxis 中的日期,所以日期重叠不是问题

数据

 Data:
structure(list(ï..State_OR_UT = c("Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Delhi", 
"Delhi", "Delhi", "Delhi", "Delhi", "Delhi", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", "Maharashtra", 
"Maharashtra", "Maharashtra"), Date = c("6/24/2020", "6/24/2020", 
"6/24/2020", "6/24/2020", "6/25/2020", "6/25/2020", "6/25/2020", 
"6/25/2020", "6/26/2020", "6/26/2020", "6/26/2020", "6/26/2020", 
"6/27/2020", "6/27/2020", "6/27/2020", "6/27/2020", "6/28/2020", 
"6/28/2020", "6/28/2020", "6/28/2020", "6/29/2020", "6/29/2020", 
"6/29/2020", "6/29/2020", "6/30/2020", "6/30/2020", "6/30/2020", 
"6/30/2020", "7/1/2020", "7/1/2020", "7/1/2020", "7/1/2020", 
"7/2/2020", "7/2/2020", "7/2/2020", "7/2/2020", "7/3/2020", "7/3/2020", 
"7/3/2020", "7/3/2020", "7/4/2020", "7/4/2020", "7/4/2020", "7/4/2020", 
"7/5/2020", "7/5/2020", "7/5/2020", "7/5/2020", "7/6/2020", "7/6/2020", 
"7/6/2020", "7/6/2020", "7/7/2020", "7/7/2020", "7/7/2020", "7/7/2020", 
"7/8/2020", "7/8/2020", "7/8/2020", "7/8/2020", "7/9/2020", "7/9/2020", 
"7/9/2020", "7/9/2020", "6/24/2020", "6/24/2020", "6/24/2020", 
"6/24/2020", "6/25/2020", "6/25/2020", "6/25/2020", "6/25/2020", 
"6/26/2020", "6/26/2020", "6/26/2020", "6/26/2020", "6/27/2020", 
"6/27/2020", "6/27/2020", "6/27/2020", "6/28/2020", "6/28/2020", 
"6/28/2020", "6/28/2020", "6/29/2020", "6/29/2020", "6/29/2020", 
"6/29/2020", "6/30/2020", "6/30/2020", "6/30/2020", "6/30/2020", 
"7/1/2020", "7/1/2020", "7/1/2020", "7/1/2020", "7/2/2020", "7/2/2020", 
"7/2/2020", "7/2/2020", "7/3/2020", "7/3/2020", "7/3/2020", "7/3/2020", 
"7/4/2020", "7/4/2020", "7/4/2020", "7/4/2020", "7/5/2020", "7/5/2020", 
"7/5/2020", "7/5/2020", "7/6/2020", "7/6/2020", "7/6/2020", "7/6/2020", 
"7/7/2020", "7/7/2020", "7/7/2020", "7/7/2020", "7/8/2020", "7/8/2020", 
"7/8/2020", "7/8/2020", "7/9/2020", "7/9/2020", "7/9/2020", "7/9/2020"
), TypeOfSales = c("Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf", "Trend - A", 
"Trend - C", "Trend - D", "Trend - Caf", "Trend - A", "Trend - C", 
"Trend - D", "Trend - Caf", "Trend - A", "Trend - C", "Trend - D", 
"Trend - Caf", "Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf", "Trend - A", 
"Trend - C", "Trend - D", "Trend - Caf", "Trend - A", "Trend - C", 
"Trend - D", "Trend - Caf", "Trend - A", "Trend - C", "Trend - D", 
"Trend - Caf", "Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf", "Trend - A", 
"Trend - C", "Trend - D", "Trend - Caf", "Trend - A", "Trend - C", 
"Trend - D", "Trend - Caf", "Trend - A", "Trend - C", "Trend - D", 
"Trend - Caf", "Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf", "Trend - A", 
"Trend - C", "Trend - D", "Trend - Caf", "Trend - A", "Trend - C", 
"Trend - D", "Trend - Caf", "Trend - A", "Trend - C", "Trend - D", 
"Trend - Caf", "Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf", "Trend - A", 
"Trend - C", "Trend - D", "Trend - Caf", "Trend - A", "Trend - C", 
"Trend - D", "Trend - Caf", "Trend - A", "Trend - C", "Trend - D", 
"Trend - Caf", "Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf", "Trend - A", 
"Trend - C", "Trend - D", "Trend - Caf", "Trend - A", "Trend - C", 
"Trend - D", "Trend - Caf", "Trend - A", "Trend - C", "Trend - D", 
"Trend - Caf", "Trend - A", "Trend - C", "Trend - D", "Trend - Caf", 
"Trend - A", "Trend - C", "Trend - D", "Trend - Caf"), ValuesTotals = c(24988L, 
39313L, 2301L, 66602L, 26588L, 41437L, 2365L, 70390L, 26586L, 
44765L, 2429L, 73780L, 28329L, 49301L, 2558L, 80188L, 28329L, 
49301L, 2558L, 80188L, 26246L, 56235L, 2680L, 85161L, 26270L, 
58348L, 2742L, 87360L, 27007L, 59992L, 2803L, 89802L, 26304L, 
63007L, 2864L, 92175L, 26148L, 65624L, 2923L, 94695L, 25940L, 
68256L, 3004L, 97200L, 25038L, 71339L, 3067L, 99444L, 25620L, 
72088L, 3115L, 100823L, 25449L, 74217L, 3165L, 102831L, 23452L, 
78199L, 3213L, 104864L, 21567L, 82226L, 3258L, 107051L, 62848L, 
69631L, 6531L, 139010L, 62369L, 73792L, 6739L, 142900L, 63357L, 
77453L, 6931L, 147741L, 67615L, 84245L, 7273L, 159133L, 67615L, 
84245L, 7273L, 159133L, 73313L, 88960L, 7610L, 169883L, 75995L, 
90911L, 7855L, 174761L, 79091L, 93154L, 8053L, 180298L, 77276L, 
101172L, 8178L, 186626L, 79927L, 104687L, 8376L, 192990L, 83311L, 
108082L, 8671L, 200064L, 86057L, 111740L, 8822L, 206619L, 87699L, 
115262L, 9026L, 211987L, 89313L, 118558L, 9250L, 217121L, 91084L, 
123192L, 9448L, 223724L, 93673L, 127259L, 9667L, 230599L)), class = "data.frame", row.names = c(NA, 
-128L))

【问题讨论】:

【参考方案1】:

实现这一点的一种方法是增加顶部的边距并通过layout 将标题定位在顶部附近,如下所示:

ggplotly(plot_sales_hp, tooltip = "text") %>% 
  layout(title = list(y = .95, xref = "plot"),
    margin = list(l = 75, t = 150)
  )

【讨论】:

以上是关于ggplotly- 绘图标题与图形重叠的主要内容,如果未能解决你的问题,请参考以下文章

Facet_Wrap标题与ggplotly中的y轴重叠?

十一. 图形图像与多媒体3.绘图模式

Matplotlib绘图时x轴标签重叠的解决办法

仅在 ggplot2 的重叠处闪避或抖动

如何调整包含绘图的 div 以绘制尺寸,使其不与文档重叠

ggplot2:移动(重新定位)图例以免阻塞图形框