How to dynamically rotate plotly pie chart to avoid overlap with title in R?

Viewed 766

I have a pie chart made from plotly that, by default looks like this with the label overlapping with the title:

enter image description here

By using setting rotation = 90, I was able to address that issue:

enter image description here

However, this situation made me wonder, what if in 3 months, "Big Projects" needs to have a protuding label instead of the current ones. Thus, I'm curious, is there a way to dynamically set these labels so, if they would overlap with the title, the chart would be rotated?

Or, alternatively, is there a way that my users of my shiny app can dynamically rotate the chart if there's an overlap?

Here's my code for a reproducible example:

library(dplyr)
library(plotly)

data <- tibble(category = c("Big Project", "Work as Usual", "Other"),
               score = c(75.4, 3.4, 1.2))

pie_chart_text_format <- list(
  family = "Arial",
  size = 11)

pie_chart_margins <- list(
  l = 40,
  r = 40,
  b = 30,
  t = 60,
  pad = 4
)

my_colors <- c("#CA001B", "#1D28B0", "#D71DA4", "#00A3AD", "#FF8200", "#753BBD", "#00B5E2", "#008578", "#EB6FBD", "#FE5000", "#6CC24A", "#D9D9D6", "#AD0C27", "#950078")





fig <- plot_ly(type='pie', labels=data$category , values=data$score, 
               textinfo='label+percent', marker = list(colors = my_colors), insidetextfont = list(color = '#FFFFFF'),
               insidetextorientation='horizontal', rotation = 90)


fig %>% layout(font=pie_chart_text_format, showlegend = FALSE, title = list(text = "Required Project Capacity\nby Type",
                                                                            font = "Arial"), margin = pie_chart_margins)
0 Answers
Related