Message Boards Message Boards

Plot multiple pie charts with individual titles and labels?


The problem I am facing is the following. I need to make a significant number of pie charts, all properly labeled and titled. I have imported a dataset, but I'm going to make it simpler here. I have this line of code:

Map[PieChart, {{3, 5, 1}, {2, 18, 10}, {1, 4, 9}}]

And I get this: enter image description here

My problem is: How do I include titles and legends for each pie chart? All of the pie charts that I have to do are for lists of the same length(i.e all of them have the same number of slices), and the legends for all the slices are the same.

POSTED BY: Jorge Mahecha
2 months ago

What do you want to do with the charts?

With data as

data = RandomInteger[{0, 25}, {5, 3}];

one approach might be

plot = PieChart[data[[#]], PlotLabel -> #, 
    ChartLegends -> {"a", "b", "c"}, 
    ChartLabels -> {"a", "b", "c"}] & /@ Range[Length@data]

which we can export using

Export[StringJoin["Chart_", IntegerString[#], ".pdf"], plot[[#]]] & /@ 
POSTED BY: David Annetts
2 months ago

Thank you, David.

This is incredibly useful. What I'm trying to do is producing reports for individual questions in a survey. Each question has a title (the actual question text). Every question in the survey has the same response categories (Strongly disagree, disagree, agree, strongly agree).

I need an easy way to customize all pie charts (i.e include titles, labels, color schemes, etc) at once or as close to "at once" as possible. In SPSS for example, it's easy to make several graphs at once. Customization has to be done individually, as far as I know.

POSTED BY: Jorge Mahecha
2 months ago

SetOptions is your friend here.

So, with (our randomised) survey data

surv = RandomInteger[{0, 25}, {25, 4}];

we can set up labels & styles

resp = {"Strongly disagree", "Disagree", "Agree", "Strongly Agree"};
styl = {Red, Lighter@Lighter@Red, Lighter@Lighter@Green, Green};

We can apply them using

  ChartLegends -> resp,
  ChartStyle -> styl,
  LabelingFunction -> "RadialCallout"

We generate all plots using

plot = PieChart[surv[[#]], 
    PlotLabel -> StringJoin["Question ", IntegerString[#]]] & /@ 

and Export[] them for reporting similar to my first response.

Documentation for PieChart shows 61 options. I suggest you experiment.

POSTED BY: David Annetts
2 months ago

This is really useful. Thanks again, David. This should be useful to a lot of people.

POSTED BY: Jorge Mahecha
2 months ago

For different options for each PieChart you can either go the Table-route or the MapThread-route:

data = {{3, 5, 1}, {2, 18, 10}, {1, 4, 9}};
plotlabels = {"plot a", "plot b", "plot c"};
MapThread[PieChart[#1, PlotLabel -> #2] &, {data, plotlabels}]

enter image description here

You can add as many variables you like (say different colors, or annotations or ....) and use #3, #4 et cetera...

POSTED BY: Sander Huisman
2 months ago

I was wondering how to set things up with MapThread. Thank you, Sander!

POSTED BY: Jorge Mahecha
2 months ago

Group Abstract Group Abstract