2
$\begingroup$

I am trying to control the overall width of a Labeled bar chart. However, it seems whether I put ImageSize->225 as an option of BarChart or as an option of Labeled, the size 225 is actually the size of the graphics object produced by BarChart and the labels will be outside this size (as shown in the figure below). When I Export the figure pic in pdf format, the width will exceed 225 due to the labels. How can I control the overall width of pic to be exactly 225? Besides, is there a way to shorten the "tick marks" of BarChart below the horizontal axis?

enter image description here

Here is the code to produce the figure:

n = Range[10];
dat1 = 1/n^2;
dat2 = 0.9 dat1;
dat = {dat1, dat2};
sty = {Orange, Black};

SetOptions[BarChart, 
  BaseStyle -> {FontFamily -> "Times New Roman", FontSize -> 8}, 
  AxesStyle -> AbsoluteThickness[0.5], LabelStyle -> Black];
SetOptions[Labeled, 
  LabelStyle -> {FontFamily -> "Times New Roman", FontSize -> 8}];

pic = Labeled[
  BarChart[Transpose@dat, ChartStyle -> {Orange, Black}, 
   BarSpacing -> {0, 1}, 
   ChartLabels -> {Placed[n, Above, Style[#, FontSize -> 8] &], None},
    AspectRatio -> 1/2, ImageSize -> 225], {"Horizontal Axis", 
   "Vertical Axis"}, {Bottom, Left}, RotateLabel -> True]
$\endgroup$
3
$\begingroup$

Why not include the labels using FrameLabel? Including the option in the BarChart function causes the funky ticks to disappear, so you can do this afterwards using Show:

g = Show[
    BarChart[
        Transpose@dat,
        ChartStyle->{Orange,Black},
        BarSpacing->{0,1},
        ChartLabels->{Placed[n,Above,Style[#,FontSize->8]&],None},
        AspectRatio->1/2,
        ImageSize->225
    ],
    Frame->{{True,False},{True, False}},
    FrameTicks->{{Automatic,None},{None,None}},
    FrameLabel->{{"Vertical Axis",None},{"Horizontal Axis",None}},
    FrameTicksStyle -> Opacity[1],
    FrameStyle -> {{None, None}, {Directive[FontOpacity -> 1, Opacity[0]], None}},
    PlotRangePadding -> {{0, Scaled[.02]}, {Scaled[.02], Scaled[.05]}}
]

enter image description here

Check that the size is correct:

Rasterize[g, "BoundingBox"]

{225, 128, 67}

$\endgroup$
  • $\begingroup$ Thanks, @Carl Woll. I have tried FrameLabel, and yeah it caused the ticks to disappear for the horizontal axis. But after Show, it looks wired to have those bars floating slightly above the axis. $\endgroup$ – nanjun May 23 at 16:01
  • $\begingroup$ @nanjun See update. I used Opacity[0] to make the horizontal frame invisible, and then added an Opacity[1] to make sure the vertical ticks appear, and adjusted the plot range padding so that there is no gap between the 0 tick on the vertical axis and the bar chart horizontal line. $\endgroup$ – Carl Woll May 23 at 16:39
  • $\begingroup$ Thank you again, @Carl Woll. Is it also possible to make those downward "tick marks" along the horizontal line shorter, e.g., half the default length? $\endgroup$ – nanjun May 23 at 17:33
  • $\begingroup$ @nanjun I think changing the length of the tick marks warrants its own question. The only method I can think of is to postprocess the graphics, something like graphic /. Line[{a_, Offset[b_, c_]}] :> Line[{a, Offset[b/2, c]}] $\endgroup$ – Carl Woll May 23 at 18:54
  • $\begingroup$ Thank you, Carl. I just asked a separate question. $\endgroup$ – nanjun May 23 at 19:51

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.