将标签放在ggplotly R中

时间:2018-10-14 11:29:31

标签: r ggplot2 rstudio plotly ggplotly

我想将甜甜圈图的标签放在其中,但我尝试了一种解决方案,但它不在我想要的位置。这是一个团队合作项目,因此任何帮助将不胜感激。谢谢。 这是我们的代码:

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))

1 个答案:

答案 0 :(得分: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))

enter image description here