1
$\begingroup$

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"};

ca[[positions]] = MapThread[Labeled[#, #2, "RadialCallout"] &, {ca[[positions]], labels}];

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

enter image description here

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?

enter image description here

EDIT

Using @kglr's solution I get the following

enter image description here

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

$\endgroup$
4
  • 1
    $\begingroup$ 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 $\endgroup$
    – kglr
    Dec 14, 2019 at 10:57
  • $\begingroup$ @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. $\endgroup$
    – Vaggelis_Z
    Dec 14, 2019 at 12:43
  • 1
    $\begingroup$ 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"}; $\endgroup$
    – kglr
    Dec 15, 2019 at 3:41
  • $\begingroup$ 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}] $\endgroup$ Feb 6, 2020 at 22:20

1 Answer 1

4
$\begingroup$

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]]}]

enter image description here

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

enter image description here

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Vaggelis_Z
    Dec 15, 2019 at 11:05
  • 1
    $\begingroup$ @Vaggelis_Z, play with larger setting for ImageSize. You might also want to add the option ContentPadding ->False in Framed. $\endgroup$
    – kglr
    Dec 15, 2019 at 11:11

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.