# Custom labels in a pie chart

Using @kglr's approach (see here) we can create the following pie chart

coloring = Thread[{-1, -2, 0, 1, 2, 9} -> {Yellow, Purple, Green, Blue, Red, Cyan}];
data = {0, 0, -1, 1, 1, 2, 9, -2, 2, 1, 1, 1, 0, 0, 9, 9, -1, 0, 9, 2};
ca = ConstantArray[1, Length@data];

positions = {5, 13, 17};
labels = {"label5", "label13", "label17"};

plot = PieChart[ca, SectorOrigin -> {{\[Pi]/2, "Clockwise"}, 0},
ChartStyle -> (data /. coloring), ChartBaseStyle -> EdgeForm[White]]


My main question: is there a way to explicitly define the size and fonts of the text labels? Perhaps adding LabelStyle -> {14, Bold, Blue}] My secondary question: in my actual data file, there are cases in which the labels are too close and the corresponding text is overlapping (see the example below). Can we do something about it?

EDIT

Using @kglr's solution I get the following

As we can see, the borders of the frame are overlapping each other.

• LabelStyle -> {14, Bold, Blue} or LabelStyle -> Directive[14, Bold, Blue] should work for styling labels. For positioning labels to avoid crowding see this answer by Silvia
– kglr
Dec 14 '19 at 10:57
• @kglr Great! Also is there a way to insert a frame around the text labels? I know that this is possible with Framed but I do not know where to insert it in your code. Dec 14 '19 at 12:43
• define labels as labels = Framed /@ {"label5", "label13", "label17"}; or (to style the frame and the backgorund) as labels = Framed[#,Background->LightBlue, FrameStyle->Red]&/@ {"label5", "label13", "label17"};
– kglr
Dec 15 '19 at 3:41
• In Callout, every bit of information of the label is important for it to figure out the proper spacing. If possible, don't use Style wrapper on the labels. This should help: ca[[positions]] = MapThread[ Callout[#, Framed@#2, LabelStyle -> {#3, 16}, Appearance -> {"Line"}] &, {ca[[positions]], labels, data[[positions]] /. coloring}] Feb 6 '20 at 22:20

I learned from this answer by rcollyer that we can use Callout with PieChart in versions 11.1+.

coloring = Thread[{-1, -2, 0, 1, 2, 9} -> {Yellow, Purple, Green, Blue, Red, Cyan}];

SeedRandom[123]
data = RandomChoice[{-1, -2, 0, 1, 2, 9}, 200];
ca = ConstantArray[1, Length @ data];


Some positions and associated labels:

positions = {1, 2, 3, 40, 170, 171, 172, 173};
labels = Style[#, 16] & /@ {"label1", "label2", "label3", "label40",
"label170", "label171", "label172", "label173"};


Wrap 1s in positions in ca with Callout using associated labels as callout label and style each label based on corresponding part of data:

ca[[positions]] = MapThread[Callout[#, Style[#2, #3], Appearance -> {"Line"}] &,
{ca[[positions]], labels, data[[positions]] /. coloring}];


Place the epilog inside the to avoid overlaps with the sector labels:

plot = PieChart[ca, SectorOrigin -> {{π/2, "Clockwise"}, 1},
ChartStyle -> (data /. coloring), ChartBaseStyle -> EdgeForm[White],
ImageSize -> Large,
Epilog -> {Line[{{0, 1}, {0, .8}}], Arrowheads[Medium],
Arrow[.9 {Cos@#, Sin@#} & /@ Subdivide[π/2, π/6, 10]]}]


Use Style[Framed@#, 16] & when defining labels and add the option ImagePadding -> 20 in PieChart to get

• I can reproduce your results with only one difference (see my edit). When I use Framed the borders of the frames overlapping each other, while in your plot they do not (there is a white gap between them). Why? Dec 15 '19 at 11:05
• @Vaggelis_Z, play with larger setting for ImageSize. You might also want to add the option ContentPadding ->False in Framed.
– kglr
Dec 15 '19 at 11:11