7
$\begingroup$

In this simple MWE, I create a simple chart element of a female figure, and then I apply it to a barchart, with the following code (note: womfig = this little stick figure here).

bd = BarChart[{0.76, 8.34},
 ChartLabels -> {"With screening", "No screening"} ,
 ChartElements -> {womfig, {1, 1}}, BarOrigin -> Left,
 PlotLabel -> Style["Lifetime cervical cancer deaths
  per 1000 women", FontSize -> 18], PlotRange -> Full,
ImageSize -> Large, BarSpacing -> 0]

This is a good start, producing this figure, but with a lot of wasted space:

Enter image description here

I have been less successful in trying to eradicate the dead space unfortunately; when I try to truncate to just the bars of interest, it creates a unwanted space between the bars.

For example, if I add PlotRange -> {{-1.5, 8.5}, {0.5, 2}}, it makes things a lot worse. I imagine this has to do with rescaling of the chart elements when you adjust the plot region: but is there an easy way to produce something like the below?

I ask because eventually this will need to take varying values, so manual tweaking is a nuisance...

Enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Notice also that portion of last figure bigger than 0.34. That's also wrong. Similarly for bottom one. It should be 0.76 of figure not hole figure. $\endgroup$ – OkkesDulgerci Jun 13 at 17:38
  • $\begingroup$ That's an excellent point - weirdly, in an old mathematica notebook for an earlier release, it seemed to work fine and produced this (correct) output... imgur.com/7mcdxnR $\endgroup$ – DRG Jun 13 at 17:45
  • 1
    $\begingroup$ AxesOrigin -> {2, 0}, PlotRange -> {Automatic, {0, 2.5}} seems to solve both your problem and the problem @OkkesDulgerci mentioned. PlotLabel could be better positioned by wrapping the plot in a Labeled function. Tested in Mathematica 12.3 $\endgroup$ – Ben Izd Jun 13 at 17:53
  • $\begingroup$ You may find this answer useful, DS 10814 $\endgroup$ – Edmund Jun 14 at 11:43
8
$\begingroup$
 womfig = Import["https://i.imgur.com/n6m1Rkc.png"]; 

1. You can use the (undocumented) option "FixedBarSpacing" to remove the extra space above and below the bars and use the spec {womfig, {1, All}} for ChartElements to have the figure stretch vertically to fill the bounding box of the bar:

 BarChart[{0.76, 8.34}, 
  ChartLabels -> {"With screening", "No screening"}, 
  ChartElements -> {womfig, {1, All}}, 
  BarOrigin -> Left, 
  PlotLabel -> Style["Lifetime cervical cancer deaths\nper 1000 women", FontSize -> 18], 
  ImageSize -> Large, 
  "FixedBarSpacing" -> True, 
  BarSpacing -> 0] 

enter image description here

Play with AspectRatio option values to get the desired proportion for vertical and horizontal lengths of bars. For example, add the option AspectRatio -> 1/4 to get:

enter image description here

2. Alternatively, use {{0.76, 8.34}} (instead of {0.76, 8.34}) as the first argument of BarChart with the option BarSpacing -> {0, 1} to get the same result:

BarChart[{{0.76, 8.34}}, 
  ChartLabels -> {"With screening", "No screening"}, 
  ChartElements -> {womfig, {1, All}},
  BarOrigin -> Left, 
  PlotLabel -> Style["Lifetime cervical cancer deaths\nper 1000 women", FontSize -> 18], 
  ImageSize -> Large, 
  BarSpacing -> {0, 1}, 
  AspectRatio -> 1/4] 

enter image description here

$\endgroup$
2
  • $\begingroup$ Fantastic, this is perfect - thank you! $\endgroup$ – DRG Jun 13 at 20:05
  • $\begingroup$ @DRG, my pleasure. $\endgroup$ – kglr Jun 13 at 20:11
2
$\begingroup$

Seems to me that you want to play a bit with AspectRatio and the size of the womfig bar.

Just a start:

enter image description here

bd = BarChart[{0.76, 8.34},
    ChartLabels -> {"With screening", "No screening"},
    ChartElements -> {womfig, {1, 1}},
    BarOrigin -> Left,
    PlotLabel -> Style["Lifetime cervical cancer deaths
        per 1000 women", FontSize -> 18],
    PlotRange -> Full,
    ImageSize -> Large, BarSpacing -> 0,
    AspectRatio -> #] & /@ {1, 0.75, 0.5, 0.25}

enter image description here

$\endgroup$

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.