I made this pie chart in R:
# https://plotly.com/r/text-and-annotations/
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv", stringsAsFactors = FALSE)
df <- df[which(df$year==2007 & df$continent=='Asia'),]
fig <- plot_ly(df, type='pie', labels = ~country, values = ~pop, textposition = 'inside')
fig1 <- fig %>% layout(uniformtext=list(minsize=12, mode='hide'))
fig1 <- fig1 %>% add_annotations(
y=1.05,
x=0.5,
text="Countries of the World",
showarrow=F,
font=list(size=15)
)
Everything works fine here, but I notice that when I am working with my real data, the title of the pie chart and the actual pie chart always come very close to intersecting with each other - I would like to try and change this.
I was thinking if there might be a way to avoid this problem. I thought that perhaps I could add more space between the title and the actual pie chart to avoid this problem from happening. I found this post here (how to adjust title space and plot plotly in r) and tried to apply the advice that was suggested in the answer:
mrg <- list(l = 50, r = 50,
b = 50, t = 50,
pad = 20)
fig1 %>% layout(margin = mrg)
However, this has not seemed to add any space between the pie chart and the title of the pie chart.
Thank you!
Here is one option making use of layout
for the title and adjusting the spacing. Then, you can just adjust the margins to get the desired spacing.
library(plotly)
plot_ly(df, type='pie', labels = ~country, values = ~pop, textposition = 'inside') %>%
layout(uniformtext=list(minsize=12, mode='hide')) %>%
layout(title = list(text = 'Countries of the World',
y=1.25,
x=0.43, font=list(size = 30)),
autosize = T,
margin=list( l = 50, r = 50, b = 100, t = 100, pad = 4))
Output