1
$\begingroup$

I have in total 16 datasets and want to make PieCharts such that the colors are appropriate for the respective item. I tried with ColorFunction but without result

Here a MWE to illustrate the problem with two datasets, pairs (item name, value):

data1 = {{"Label 1", 2}, {"Label 2", 4}, {"Label 3", 4}};
data2 = {{"Label 1", 2}, {"Label 2", 4}, {"Label 4", 4}};

so

allLabels = {"Label 1", "Label 2", "Label 3", "Label 4"};

colors = Hue /@ (Range[4]/4);

So we get the desired colorings:

enter image description here

and the colorChooser:

colorChooser[label_String] := 
 Select[labelColors, First[#] == label &][[1, 2]]

Then:

PieChart[data1[[All, 2]], ChartLegends -> data1[[All, 1]],
 ColorFunction -> (colorChooser[#,1] &); (*Choose color for the item*)
 ChartElementFunction -> "PlateauSector",
 ChartLabels -> relativeData[[All, 2]]
 ]

delivers:

enter image description here

and

PieChart[data2[[All, 2]], ChartLegends -> data2[[All, 1]],
 ColorFunction -> (colorChooser[#,1] &); (*Choose color for the item*)   
 ChartElementFunction -> "PlateauSector",
 ChartLabels -> data2[[All, 2]]
 ]

enter image description here

The desired output would be that the items are colored according to the colortable, so "item 4" should be in red. How can I set up the ChartLegends and the colorChooser appropriate?

$\endgroup$
2
$\begingroup$
coloring = Thread[allLabels -> colors];
PieChart[#[[All, 2]], ChartStyle -> #[[All, 1]] /. coloring, 
    ChartLabels -> #[[All, 1]], ChartLegends -> #[[All, 1]], 
    ChartElementFunction -> "PlateauSector", ImageSize -> 300] & /@ {data1, data2} // Row

enter image description here

Alternatively, wrap each data point with Style and Labeled:

{styledD1, styledD2} = Style[Labeled[#2, #], # /. coloring] & @@@ # & /@ {data1, data2};
PieChart[#, ChartLegends -> SwatchLegend[#[[All, 2]], #[[All, 1, 2]]],
  ChartElementFunction -> "PlateauSector", ImageSize -> 300] & /@ {styledD1, styledD2} // 
  Row

same picture

$\endgroup$
  • $\begingroup$ Thank you for this fast solution - and it is so "easy" (if you know it...) $\endgroup$ – mgamer May 4 '18 at 10:11
  • $\begingroup$ @mgamer, my pleasure. Thank you for the accept. $\endgroup$ – kglr May 4 '18 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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