3
$\begingroup$

I want to construct a grid of pie charts but I want the legend (which is common to all charts) to appear at the bottom of the grid.I include the code and a picture of the output.

Code updated with answer

Legended[Grid[{{PieChart[{1, 2, 3, 0}, ChartStyle -> ColorData[63], 
LabelingFunction -> "RadialOutside"], 
PieChart[{0, 4, 5, 6}, 
LabelingFunction -> "RadialOutside"]}, {PieChart[{7, 0, 8, 9}, 
LabelingFunction -> "RadialOutside"], SpanFromLeft}}], 
Placed[SwatchLegend[97, {"Eric", "Kyle", "Stan", "Kenny"}, 
LegendLayout -> "Row"], Below]]

A - SOLVED

This code does most of what I want to do, but essentially I would like

1) The legend under the first pie chart to replace the ABCD legend.

2) The numerical value of each slice to appear on the outside of the slice.

The catch is in the construction of the pie charts. From a set of X variables only some appear in each chart, but I need the legend to include all of them (notice the rotating 0 values in the code). In other words, I need the colour and label in the legend to correspond to the same quantity (if it appears) along all charts.

B - SOLVED

On a side note, my data is an n*m table where each pie chart corresponds to a row. Is there a way to streamline the above process by using Grid or Table, but when n number of charts are in a grid row a new line starts?

Code updated with answer

data = {{0, 1, 2, 3}, {2, 4, 5, 6}, {4, 3, 5, 3}}; charts = 
PieChart[#, ChartStyle -> "DarkBands",LabelingFunction -> "RadialOutside"] & /@ data; Legended[
Grid[Partition[charts, 2, 2, {1, 1}, SpanFromLeft]], 
Placed[SwatchLegend["DarkBands", {"Eric", "Kyle", "Stan", "Kenny"}, 
LegendLayout -> "Row"], Below]]

The problem I have is having the same colour corresponding to the same variable across the charts, as above.

Many thanks to N.J.Evans for his help!

$\endgroup$

2 Answers 2

4
$\begingroup$

I would recommend using Grid instead of GraphicsGrid, omit the legend from the first chart, also note that the base style uses ColorData[97], if you want to use 63 you can change the style by hand as I did for the first chart:

Legended[
 Grid[{
   {PieChart[{1, 2, 3, 0},ChartStyle->ColorData[97], LabelingFunction -> "RadialEdge"], 
    PieChart[{0, 4, 5, 6}, LabelingFunction -> "RadialEdge"]},
   {PieChart[{7, 0, 8, 9}, LabelingFunction -> "RadialEdge"], 
    SpanFromLeft}}]
 ,
 Placed[SwatchLegend[97, {"Eric", "Kyle", "Stan", "Kenny"}, LegendLayout -> "Row"],
   Below]
 ]

Which produces

enter image description here

As long as your legend includes all the elements from your chart in the correct order, they should all be represented.

$\endgroup$
4
  • 1
    $\begingroup$ Thank you very much! That solves it, after replacing RadialEdge with RadialOutside. Is there any way to streamline the process as I describe in Part B in my question, i.e. insert data automatically? $\endgroup$
    – Titus
    Nov 14, 2018 at 15:26
  • $\begingroup$ If you can provide some sample data, and specify the layout you would like, it should be pretty straightforward. I'd probably take this code and your data and open another question addressing that issue specifically, it will make the questions more focused and useful for other users. $\endgroup$
    – N.J.Evans
    Nov 14, 2018 at 15:28
  • $\begingroup$ The layout is exactly the same as in your answer and any toy matrix would serve as data, like the one in my example in the OP. I will try and solve it myself (e.g. by using the example of @b.gatessucks), but any help is appreciated! $\endgroup$
    – Titus
    Nov 14, 2018 at 15:34
  • 1
    $\begingroup$ data = {{0, 1, 2, 3}, {2, 4, 5, 6}, {4, 3, 5, 3}}; charts = PieChart[#, LabelingFunction -> "RadialEdge"] & /@ data; Legended[ Grid[Partition[charts, 2, 2, {1, 1}, SpanFromLeft]], Placed[SwatchLegend[97, {"Eric", "Kyle", "Stan", "Kenny"}, LegendLayout -> "Row"], Below]] $\endgroup$
    – N.J.Evans
    Nov 14, 2018 at 16:11
2
$\begingroup$

I would have a more compact representation:

names = {"eric", "kyle", "stan", "kenny"};;    
data = <|"a" -> AssociationThread[names -> {1, 2, 3, 0}], 
  "b" -> AssociationThread[names -> {0, 4, 5, 6}], 
  "c" -> AssociationThread[names -> {7, 0, 8, 9}]|>;

PieChart[data, ChartLegends -> Placed[Automatic, Below], LabelingFunction -> "RadialEdge"]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you, but my actual number of charts is quite larger and the colours are not continuous. I tried out something similar but it was confusing to read. $\endgroup$
    – Titus
    Nov 14, 2018 at 14:50

Your Answer

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

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