Поместите метки в ggplotly R

Я хотел бы поместить метки своих кольцевых диаграмм посередине этих диаграмм, но я попробовал решение, и оно не появляется там, где я хочу. Это для командного проекта, поэтому любая помощь будет очень благодарна. Спасибо. Вот наш код:

p1 <- plot_ly(tab2, labels = ~c("Domicile-Travail","Domicile-ecole","Courses-achats","Utilisation professionnelle","Promenade-loisirs","Autre"), values = ~prop, type = 'pie',textposition = 'middle right',
         textinfo = 'percent',subtitle="B", hole= "0.5",domain = list(x = c(0, 0.45), y = c(0, 1)),insidetextfont = list(color = '#FFFFFF'),
         hoverinfo = 'text')

p2 <- plot_ly(tab3, labels= ~c("Domicile-Travail","Domicile-ecole","Courses-achats","Utilisation professionnelle","Promenade-loisirs","Autre"), values = ~prop, type = 'pie', textposition = 'middle left',
              textinfo = 'percent',hole= "0.5", domain = list(x = c(0.55,1), y = c(0, 1)), insidetextfont = list(color = '#FFFFFF'),
              hoverinfo = 'text', annotations=list(text="B",x=0,y=0,label=2013))

subplot(p1,p2)%>% 
  layout(title = "Nombre d'accidents par type de trajet ", 
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), 
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

person TWCars    schedule 14.10.2018    source источник
comment
Не могли бы вы поделиться своими tab2 и tab3 объектами?   -  person Marco Sandri    schedule 14.10.2018
comment
Спасибо за ваш ответ, я новичок на этом веб-сайте, поэтому я не знаю, как поделиться csv, я добавлю его в следующем ответе   -  person TWCars    schedule 14.10.2018
comment
tab 2 trajet n prop 1 1 21654 0,15 2 2 3373 0,02 3 3 4443 0,03 4 4 20151 0,14 5 5 73990 0,53 6 9 16352 0,12 tab 3 trajet n prop 1 1 17743 0,19 2 2 2886 0,03 3 3 3341 0,04 4 4 12911 0,14 5 5 47726 0,52 6 9 7171 0,08   -  person TWCars    schedule 14.10.2018


Ответы (1)


Это возможное решение с использованием annotations:

library(plotly)

tab2 <- read.table(text="
trajet n prop 
1 21654 0.15
2 3373 0.02
3 4443 0.03
4 20151 0.14
5 73990 0.53
9 16352 0.12 
", header=T)

tab3 <- read.table(text="
trajet n prop
1 17743 0.19
2 2886 0.03
3 3341 0.04
4 12911 0.14
5 47726 0.52
9 7171 0.08
", header=T)

subtitle1 <- list(
  x = 0.5-0.025,  y = 0.5,
  text = "Title 1",
  xref = "paper", yref = "paper",
  font=list(size=50),
  xanchor="center", yanchor="center",
  showarrow = FALSE
)

p1 <- plot_ly(tab2, 
         labels = ~c("Domicile-Travail","Domicile-ecole","Courses-achats",
                     "Utilisation professionnelle","Promenade-loisirs","Autre"), 
         values = ~prop, type = 'pie', 
         textposition = 'middle right',
         textinfo = 'percent', 
         hole= "0.5",
         domain = list(x = c(0, 0.45), y = c(0, 1)),
         insidetextfont = list(color = '#FFFFFF'),
         hoverinfo = 'text') %>%
         layout(annotations = subtitle1)


 subtitle2 <- list(
  x = 0.5+0.025,  y = 0.5,
  text = "Title 2",
  xref = "paper",  yref = "paper",
  font=list(size=50),
  xanchor="center",  yanchor="center",
  showarrow = FALSE
)

p2 <- plot_ly(tab3, 
              labels= ~c("Domicile-Travail","Domicile-ecole","Courses-achats",
                         "Utilisation professionnelle","Promenade-loisirs","Autre"), 
              values = ~prop, 
              type = 'pie', 
              textposition = 'middle left',
              textinfo = 'percent', 
              hole= "0.5", 
              domain = list(x = c(0.55,1), y = c(0, 1)), 
              insidetextfont = list(color = '#FFFFFF'),
              hoverinfo = 'text') %>% 
              layout(annotations = subtitle2)


subplot(p1,p2)%>% 
  layout(title = "Nombre d'accidents par type de trajet", 
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE), 
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

введите здесь описание изображения

person Marco Sandri    schedule 14.10.2018