4
$\begingroup$

I'm trying to reproduce the data visualizations in this article: https://towardsdatascience.com/improve-your-analytics-projects-w-these-data-distributions-visualizations-7ba3821f2092

The composite pie chart on the right in the "Pie Chart" section is giving me trouble. I have stored the source data in a dataset:

dsPie = Dataset[<|"Existing Customer" -> <|3 -> 0.188111, "More3" -> 0.495803, 
 "Less2incl" -> 0.155426|>, 
"Attrited Customer" -> <|3 -> 0.0394984, "More3" -> 0.0639874, 
  "Less2incl" -> 0.0571739|>|>]

I started with trying to reproduce the innermost chart. I have the following code:

dsPie[Transpose][
d |-> PieChart[d, ChartLabels -> Automatic, 
ChartLegends -> Automatic, 
 LabelingFunction -> (Placed[#3 // First // First, 
   "RadialCallout"] &), ChartLayout -> "Stacked"]]

But the visual is cluttered. How can I go further?

$\endgroup$
1

2 Answers 2

4
$\begingroup$

The pie-chart you mentioned consist of two data series:

enter image description here

Image source

  1. extract inner circle values as List
tempa = Values /@ Normal @ Values @ dsPie[Transpose]
(*Out: {{0.188111, 0.0394984}, {0.495803, 0.0639874}, ...} *)
  1. Map Total on them to create outer circle:
b = Total /@ tempa
(*Out: {0.227609, 0.55979, 0.2126} *)
  1. Coloring inner circle pairs + flattening the array
a = Flatten[{Style[#[[1]], LightOrange], Style[#[[2]], LightBlue]} & /@ tempa];
  1. Define a function to do the labeling
labeler[v_, __] := Placed[{PercentForm[v, 3]}, {"RadialOuter"}]
  1. Draw chart
PieChart[{a, b}, SectorOrigin -> {Automatic, 1}, LabelingFunction -> labeler, ChartStyle -> {LightRed, LightPurple, LightGreen}]

enter image description here

Whole code:

dsPie = Dataset[<|
   "Existing Customer" -> <|3 -> 0.188111, "More3" -> 0.495803, 
     "Less2incl" -> 0.155426|>, 
   "Attrited Customer" -> <|3 -> 0.0394984, "More3" -> 0.0639874, 
     "Less2incl" -> 0.0571739|>|>]

tempa = Values /@ Normal@Values@dsPie[Transpose]

b = Total /@ tempa

a = Flatten[{Style[#[[1]], LightOrange], Style[#[[2]], LightBlue]} & /@ tempa];

labeler[v_, __] := Placed[{PercentForm[v, 3]}, {"RadialOuter"}]

PieChart[{a, b}, SectorOrigin -> {Automatic, 1}, LabelingFunction -> labeler, ChartStyle -> {LightRed, LightPurple, LightGreen}]

$\endgroup$
2
  • $\begingroup$ So, using Style will override default choices for sector colors? $\endgroup$
    – Whelp
    Feb 8, 2021 at 16:55
  • 1
    $\begingroup$ Yes. according to ChartStyle documentation: "Style[data,s] can be used to style chart elements associated with individual data points or datasets. Such specifications are used after specifications from ChartStyle." $\endgroup$
    – Ben Izd
    Feb 8, 2021 at 18:47
3
$\begingroup$
dsPie = Dataset[<|
    "Existing Customer" -> <|
      3 -> 0.188111,
      "More3" -> 0.495803,
      "Less2incl" -> 0.155426|>,
    "Attrited Customer" -> <|
      3 -> 0.0394984,
      "More3" -> 0.0639874,
      "Less2incl" -> 0.0571739|>|>];

dsPie[d |-> PieChart[d,
   ChartLabels -> {Style[#, 14, 
        Bold] & /@ {"Existing     \nCustomers", 
       "Attrited      \nCustomers"}, Automatic},
   ChartLayout -> "Grouped",
   SectorOrigin -> {{-3 Pi/4, "Clockwise"}, 1/4},
   ImageSize -> 400]]

enter image description here

$\endgroup$
1
  • $\begingroup$ I like the in-line code, but I believe this chart does not fully reproduce the target. The inner chart should show both Attrited and Existing Customers, while the outer one shows the aggregation. $\endgroup$
    – Whelp
    Feb 8, 2021 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.