@@ -4153,8 +4153,10 @@ makeSubColor=function(main,no=3){
41534153# 'Draw a PieDonut plot
41544154my.PieDonut = function (data ,mapping ,
41554155 start = getOption(" PieDonut.start" ,0 ),
4156- addPieLabel = TRUE ,addDonutLabel = TRUE ,
4157- showRatioDonut = TRUE ,showRatioPie = TRUE ,
4156+ addPieLabel = TRUE ,
4157+ addDonutLabel = TRUE ,
4158+ showRatioDonut = TRUE ,
4159+ showRatioPie = TRUE ,
41584160 ratioByGroup = TRUE ,
41594161 showRatioThreshold = getOption(" PieDonut.showRatioThreshold" ,0.02 ),
41604162 labelposition = getOption(" PieDonut.labelposition" ,2 ),
@@ -4175,18 +4177,25 @@ my.PieDonut=function(data,mapping,
41754177 pieLabelSize = 4 ,
41764178 donutLabelSize = 3 ,
41774179 titlesize = 5 ,
4178- explodePie = TRUE ,explodeDonut = FALSE ,
4179- use.label = TRUE ,use.labels = TRUE ,
4180+ explodePie = TRUE ,
4181+ explodeDonut = FALSE ,
4182+ use.label = TRUE ,
4183+ use.labels = TRUE ,
41804184 family = getOption(" PieDonut.family" ," " ),
41814185 labels = NULL ,
41824186 main.colors = NULL ,
4187+ sub.colors = NULL ,
41834188 correct.legend.x = NULL ,
41844189 correct.legend.y = NULL ,
41854190 tidy.legend.donut = NULL ,
41864191 tidy.legend.pie = NULL ,
41874192 plot.piedonut = TRUE ,
41884193 ratioAccuracy = c(" pie" = 0.1 , " donut" = 0.1 ),
4189- return = " both" ){
4194+ return = " both" ,
4195+ legend.pie = FALSE ,
4196+ legend.donut = FALSE ,
4197+ explode.cor.pie = 1 ,
4198+ explode.cor.donut = 1 ){
41904199
41914200 # data = pie.df
41924201 # mapping = aes(category, main.criteria)
@@ -4199,8 +4208,8 @@ my.PieDonut=function(data,mapping,
41994208 # family = ""
42004209 # main.colors = cores[-1]
42014210
4202- (cols = colnames(data ))
4203- if (use.labels ) data = moonBook :: addLabelDf(data ,mapping )
4211+ (cols = colnames(data ))
4212+ if (use.labels ) data = moonBook :: addLabelDf(data ,mapping )
42044213
42054214 count <- NULL
42064215
@@ -4242,8 +4251,8 @@ my.PieDonut=function(data,mapping,
42424251 df $ focus = 0
42434252 if (explodePie ) df $ focus [explode ]= explodePos
42444253 df $ mid = (df $ start1 + df $ end1 )/ 2
4245- df $ x = ifelse(df $ focus == 0 ,0 ,df $ focus * sin(df $ mid ))
4246- df $ y = ifelse(df $ focus == 0 ,0 ,df $ focus * cos(df $ mid ))
4254+ df $ x = ifelse(df $ focus == 0 ,0 ,df $ focus * sin(df $ mid ) * explode.cor.pie )
4255+ df $ y = ifelse(df $ focus == 0 ,0 ,df $ focus * cos(df $ mid ) * explode.cor.pie )
42474256 df $ label = df [[pies ]]
42484257 df $ ratio = df $ Freq / sum(df $ Freq )
42494258
@@ -4256,8 +4265,7 @@ my.PieDonut=function(data,mapping,
42564265 # as.character(df$label))
42574266 df $ label = paste0(df $ label , " (" , scales :: percent(df $ ratio , accuracy = ratioAccuracy [" pie" ]), " )" )
42584267 df $ label [tidy.legend.pie ] = gsub(" \\ (" , " \n (" ,df $ label [tidy.legend.pie ])
4259-
4260-
4268+
42614269 }
42624270
42634271 if (is.null(correct.legend.x )) {
@@ -4292,7 +4300,6 @@ my.PieDonut=function(data,mapping,
42924300 subColor = makeSubColor(mainCol , no = length(unique(data [[donuts ]])))
42934301 subColor
42944302
4295-
42964303 data
42974304 if (! is.null(count )){
42984305
@@ -4380,8 +4387,8 @@ my.PieDonut=function(data,mapping,
43804387 xpos = df $ focus [explode [i ]]* sin(df $ mid [explode [i ]])
43814388 ypos = df $ focus [explode [i ]]* cos(df $ mid [explode [i ]])
43824389
4383- df3 $ x [df3 $ pie == explode [i ]]= xpos
4384- df3 $ y [df3 $ pie == explode [i ]]= ypos
4390+ df3 $ x [df3 $ pie == explode [i ]] = xpos * explode.cor.donut
4391+ df3 $ y [df3 $ pie == explode [i ]] = ypos * explode.cor.donut
43854392 }
43864393 }
43874394 df3 $ no = 1 : nrow(df3 )
@@ -4442,6 +4449,22 @@ my.PieDonut=function(data,mapping,
44424449 }
44434450 df3
44444451
4452+ if (! is.null(sub.colors )) {
4453+ no.sub <- length(sub.colors )
4454+ no <- length(unique(data [[donuts ]]))
4455+
4456+ if (no.sub == length(subColor )) subColor <- sub.colors
4457+
4458+ if (no.sub != no )
4459+ stop(cat(" Length of subcolors is different than the number of" , donuts ))
4460+
4461+ if (no.sub == no ) {
4462+ cores <- sub.colors
4463+ names(cores ) <- sort(unique(data [[donuts ]]))
4464+ subColor <- unname(cores [match(df3 [[donuts ]], names(cores ))])
4465+ }
4466+ }
4467+
44454468 del = which(df3 $ Freq == 0 )
44464469 del
44474470 if (length(del )> 0 ) subColor <- subColor [- del ]
@@ -4462,15 +4485,16 @@ my.PieDonut=function(data,mapping,
44624485 fill = pies ),alpha = pieAlpha ,color = color , data = df ) +
44634486 transparent()+
44644487 scale_fill_manual(values = mainCol )+
4465- xlim(r3 * c(- 1.2 ,1.2 )) + ylim(r3 * c(- 1 ,1 )) + guides(fill = FALSE ) +
4488+ xlim(r3 * c(- 1.2 ,1.2 )) + ylim(r3 * c(- 1 ,1 )) +
4489+ guides(fill = legend.pie ) +
44664490 theme_void()
44674491
44684492 if ((labelposition == 1 ) & (is.null(donuts ))) {
44694493 p1 <- p1 + geom_segment(aes_string(x = " segx" , y = " segy" ,
44704494 xend = " segxend" ,yend = " segyend" ),data = df )+
44714495 geom_text(aes_string(x = " segxend" ,y = " segyend" ,label = " label" ,hjust = " hjust" ,vjust = " vjust" ),size = pieLabelSize ,data = df ,family = family )
44724496
4473- } else
4497+ } else {
44744498 if ((labelposition == 2 ) & (is.null(donuts ))) {
44754499 p1 <- p1 + geom_segment(aes_string(x = " segx" ,y = " segy" ,
44764500 xend = " segxend" ,yend = " segyend" ),data = df [df $ ratio < labelpositionThreshold ,])+
@@ -4486,6 +4510,7 @@ my.PieDonut=function(data,mapping,
44864510 family = family
44874511 )
44884512 }
4513+ }
44894514
44904515 if (showPieName ) p1 <- p1 + annotate(" text" ,x = 0 ,y = 0 ,label = pies ,size = titlesize ,family = family )
44914516
@@ -4508,8 +4533,9 @@ my.PieDonut=function(data,mapping,
45084533 }
45094534
45104535 p3 <- p3 + transparent()+
4511- scale_fill_manual(values = subColor )+
4512- xlim(r3 * c(- 1 ,1 ))+ ylim(r3 * c(- 1 ,1 ))+ guides(fill = FALSE )+
4536+ scale_fill_manual(values = subColor ) +
4537+ xlim(r3 * c(- 1 ,1 ))+ ylim(r3 * c(- 1 ,1 )) +
4538+ guides(fill = legend.donut ) +
45134539 theme_void()
45144540
45154541 p3
@@ -4519,7 +4545,7 @@ my.PieDonut=function(data,mapping,
45194545 xend = " segxend" ,yend = " segyend" ),data = df3 )+
45204546 geom_text(aes_string(x = " segxend" ,y = " segyend" ,
45214547 label = " label" ,hjust = " hjust" ,vjust = " vjust" ),size = donutLabelSize ,data = df3 ,family = family )
4522- } else
4548+ } else {
45234549 if (labelposition == 0 ){
45244550 p3 <- p3 + geom_text(aes_string(x = " labelx" ,y = " labely" ,
45254551 label = " label" ),size = donutLabelSize ,data = df3 ,family = family )
@@ -4532,6 +4558,7 @@ my.PieDonut=function(data,mapping,
45324558 label = " label" ),size = donutLabelSize ,data = df3 [df3 $ ratio1 > = labelpositionThreshold ,],family = family )
45334559
45344560 }
4561+ }
45354562
45364563 if (! is.null(title ))
45374564 p3 <- p3 + annotate(" text" ,x = 0 ,y = r3 ,label = title ,size = titlesize ,family = family )
0 commit comments