1

I'm trying to improve the piedonut function I copied from the post here. The problem is to put labels in the center of the pieces of the inner (main) group of the donut. Here's an example for better understanding.

piedonut <- function(x_outer, 
                     group_inner=NULL, 
                     labels_inner=NULL,
                     labels_outer=NULL,  
                     border_outer=NULL,
                     col=NULL, 
                     col_labels_inner=NULL,
                     radius = c(.7, 1)){
  group <- rep_len(group_inner, 
                   length(x_outer))
  ug  <- unique(group)
  tbl <- table(factor(group, 
                      levels=ug))

  col <- if (is.null(col)){
    seq_along(ug)
    }else{
    rep_len(col, length(ug))
    }
  col.main <- Map(rep, 
                  col[seq_along(tbl)], 
                  tbl)
  col.sub  <- lapply(col.main, 
                     function(x){
                       al <- head(seq(0, 1, length.out = length(x) + 2L)[-1L], 
                                  -1L)
                       Vectorize(adjustcolor)(x, alpha.f = al)
   }
  )

  plot.new()

  ##OUTER PLOT -> Donut
  par(new=TRUE)
  pie(x_outer, 
      border=border_outer, 
      radius=radius[2L],
      col=unlist(col.sub), 
      labels=labels_outer)

  ##INNER PLOT -> Pie
  par(new=TRUE)
  x_aux <- split(x_outer,
                 rep(1:3,c(tbl)))
  x_aux1 <- lapply(x_aux,
                   mean)
  x_aux2 <- unlist(x_aux1)
  x_mean <- c(0,cumsum(x_aux2)/sum(x_aux2))
  nx <- length(x_aux)
  
  twopi <- 2*pi
  
  t2xy <- function(t) {
          t2p <- twopi * t
          list(x = radius[1L] * cos(t2p), 
               y = radius[1L] * sin(t2p))
  }
  
  pie(x_outer, 
      border=NA, 
      radius=radius[1L],
      col=unlist(col.main), 
      labels=NA)
  if(is.null(labels_inner)){
    labels_inner <- ug
  }
  if(is.null(col_labels_inner)){
    col_labels_inner <- 1:nx
  }
  for(i in 1L:nx){
     P <- t2xy(mean(x_mean[i + 0:1]))
     text((0.35 * P$x), 
          (0.35 * P$y), 
          labels_inner[i], 
          xpd=TRUE,
          adj=0.5,
          col=col_labels_inner)
     #points(1.1*P$x, 1.1*P$y,col='red',size=5)                    
 }   
}

dados <- structure(list(fatind = c("NHOMICIDIO", 
"NOCORRENCIAS", "IDH", "IVS", "DENSIDADE", "NPRISAOFLAGRANTE", 
"NTCO", "POLI_HAB"), percind = c(0.75, 0.25, 0.333, 0.167, 0.5, 
0.3, 0.3, 0.4), group = c("est_cri", "est_cri", "ind_socioec", 
"ind_socioec", "ind_socioec", "prod_pol", "prod_pol", "prod_pol"
), percgroup = c(0.273, 0.273, 0.273, 0.273, 0.273, 0.455, 0.455, 
0.455)), row.names = c(46L, 47L, 75L, 76L, 77L, 10L, 11L, 12L
), class = "data.frame")

Notice that the sum of the percentages (percind) within the "group" factor adds up to one and the percentage (percgroup) sum of the groups sums up to one.

with(dados,
     piedonut(x_outer=percind, 
              group_inner=group,
              labels_outer=fatind,
              labels_inner=unique(dados$group),
              col_labels_inner='white',
              border_outer='black'))

0 Answers0