2
$\begingroup$

The following code works just fine with a few caveats.

a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
cl = {"Afghanistan", "Bangladesh", "Bhutan", "Cambodia", "Kiribati", 
   "Lao DPR", "Myanmar", "Nepal", "Samoa", "Solomon Islands", 
   "Timor-Leste", "Tuvalu", "Vanuatu"};
bchart1 = BarChart[
   a,
   ChartLabels -> cl,
   ChartStyle -> LightBlue,
   PlotTheme -> "Business",
   BarOrigin -> Left,
   PlotLabel -> Style["GDP per capita, 2011", Black, Bold, 12],
   BarSpacing -> 0.3,
   AxesLabel -> {"Millions US$"},
   ImageSize -> 350,
   Background -> LightGray
   ];
line1 = ParametricPlot[
   {Mean[a], u}, {u, 0.3, Max[a]},
   PlotRange -> {{0, Max[a]}, {0, Max[a]}},
   PlotStyle -> {Directive[Black, Dashed]}
   ];
Show[bchart1, line1]

It produces:

enter image description here

There are three places that need to be corrected.

  1. Background color should be a little lighter than it is now;
  2. The vertical dashed line is the mean value of the chart values indicated. As one may see, the dashed line does not extend to the edge of the first bar from the top. The line should cover the entire range of the bars and also pass through the horizontal line just a bit and have a label "Average"; and
  3. Somehow AxesLabel does not place the x-axis label.

Thank you.

$\endgroup$
5
$\begingroup$
a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
cl = {"Afghanistan", "Bangladesh", "Bhutan", "Cambodia", "Kiribati", 
   "Lao DPR", "Myanmar", "Nepal", "Samoa", "Solomon Islands", 
   "Timor-Leste", "Tuvalu", "Vanuatu"};

mu = Mean[a];

bchart1 = BarChart[Tooltip /@ a,
  ChartLabels -> cl,
  ChartStyle -> LightBlue,
  PlotTheme -> "Business",
  BarOrigin -> Left,
  PlotLabel -> Style["GDP per capita, 2011", Black, Bold, 12],
  BarSpacing -> 0.3,
  FrameLabel -> {None, "Millions US$"},
  ImageSize -> 400,
  Background -> Lighter[Gray, 0.9], (* adjust to your preference *)
  Epilog -> {Dashed,
    Tooltip[Line[{{mu, -0.005}, {mu, Max[a] + 0.45}}], mu],
    Text["Average", {mu, -0.025}, {0, 1}]},
  PlotRangeClipping -> False]

enter image description here

EDIT: Added sort capability (ReverseSortBy was introduced in version 12.0; for versions 10.0+, use SortBy /* Reverse)

Manipulate[
 Module[{sortedData,
   data = Transpose[{cl, a}]},
  sortedData = If[sort > 0, SortBy, ReverseSortBy][data, #[[Abs[sort]]] &];
  bchart1 = BarChart[Tooltip /@ sortedData[[All, 2]],
    ChartLabels -> sortedData[[All, 1]],
    ChartStyle -> LightBlue,
    PlotTheme -> "Business",
    BarOrigin -> Left, 
    PlotLabel -> Style["GDP per capita, 2011",
      Black, Bold, 12],
    BarSpacing -> 0.3,
    FrameLabel -> {None, Row[{Spacer[250], "Millions US$"}]},
    ImageSize -> 400,
    Background -> Lighter[Gray, 0.9],(*adjust to your preference*)
    
    Epilog -> {Dashed, Tooltip[Line[{{mu, -0.005}, {mu, Max[a] + 0.45}}], mu],
       Text["Average", {mu, -0.025}, {0, 1}]}, PlotRangeClipping -> False]],
 {{sort, 1, "Sort By"}, {1 -> "Alpha", (-1) -> "Reverse Alpha", 
   2 -> "GDP", (-2) -> "Reverse GDP"}}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks for the code. It works very nicely. Could you also tell me how to move the X-axis label "Millions US$" to the right of the frame? Currently, it looks a bit inconvenient. $\endgroup$ Oct 17 at 12:04
  • $\begingroup$ Since I have many graphs like this, I want to keep the Y-axis sorted, alphabetically sorted from top to bottom, starting from Afghanistan to Vanuatu at the bottom. $\endgroup$ Oct 17 at 12:11

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.