Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a fairly complex sort of BarChart that I want to overlay with other graphics. For concreteness, let's assume that I want to overlay a LinePlot. In doing so, I found this answer quite helpful, but my chart has some additional wrinkles which complicate things. First, let's invent some data:

data = {Association["a" -> 0.7037417451289036, 
    "b" -> 0.503687396357559, "c" -> 0.8305301936239331, 
    "d" -> 0.23466296631845518], Association[
    "a" -> 0.08369185073518004, "b" -> 0.5808357340946317, 
    "c" -> 1.873919922991976, "d" -> 1.3981710526525974], 
   Association["a" -> 1.6327509774236528, "b" -> 1.3925276568460028, 
    "c" -> 1.1036001759467897, "d" -> 1.0264775292307071], 
   Association["a" -> 3.218464871800018, "b" -> 0.6400499501591295, 
    "c" -> 0.4775621030459698, "d" -> 2.375619997842695], 
   Association["a" -> 3.160797696474269, "b" -> 0.36822672048523586, 
    "c" -> 3.862723785325967, "d" -> 0.41766552167593396]};     

Then I'll whip up a BarChart:

bars = BarChart[data, ChartLegends -> Automatic, 
  PlotRange -> {Automatic, Automatic}]

so a duck walks into a bar...

Now for the ListPlot, using the DataRange trick suggested by that linked answer.

means = Mean /@ data;

lines = ListPlot[means, Joined -> True, PlotStyle -> Black, 
  DataRange -> {2.5, 2.5 + 4*4}]

enter image description here

Now I can Show the two together:

Show[bars, lines, ImagePadding -> {{10, 10}, {10, 10}}]

enter image description here

As you can see, it's almost, but not quite, right. Evidently the spacing between the groups throws things off, but I'm not entirely sure how much, in part because I'm not sure how much spacing there is between bars. It's complicated by the fact that there are two different sorts of spacing, one within groups of bars, and one between bars, and the two seem to affect the $x$-coördinates of the bars in different ways. I could set BarSpacings to {None, None}, but that looks ugly.

Is there a better way? Or a simple way of understanding how bars are spaced when they're divided into groups?

share|improve this question
1  
related: mathematica.stackexchange.com/questions/77784 – Edmund Nov 6 '15 at 22:00

Perhaps this:

Get the tick values:

ticks = FrameTicks /. Options[First @ bars, FrameTicks]

Get the x tick values:

 xticks = ticks[[2, 1, All, 1]]

Visualize the x tick values with dots on top of the bar chart:

Show[bars, Graphics[{AbsolutePointSize[4], Point[{#, 0}]} & /@ xticks]]    

enter image description here

This gives you all the center points of each bar, but also the center points between the groups of bars. Get rid of those:

xticks2 = Drop[xticks, {5, -1, 5}]

Using those x ticks values, use the first and last of each group to find the center x coordinate of each group:

line = Table[
 Line[{
  {(xticks2[[4 i]] + xticks2[[4 i - 3]])/2, means[[i]]}, 
  {(xticks2[[4 (1 + i)]] + xticks2[[1 + 4 i]])/2, means[[i + 1]]}}
 ], 
{i, 1, 4}]

Make the new plot, with the line points centered on the bar groups:

Show[bars, Graphics[{AbsoluteThickness[5], line}]]

enter image description here

share|improve this answer
    
+1, since it's pretty clever. Still, it fails if FrameTicks has been replaced with a custom value, which is a possibility. – Pillsy Nov 6 '15 at 22:48
    
You can probably make this work with any custom FrameTicks, but I haven't tried that case. – Arnoud Buzing Nov 6 '15 at 22:54

Maybe here's a small improvement

Show[
 bars,
 ListLinePlot[means, DataRange -> {2.6, 19}],
 ListPlot[means, DataRange -> {2.6, 19}],
 GridLines -> {{2.6, 6.7, 10.8, 14.9, 19}, {1, 2, 3, 4}},
 ImageSize -> Large,
 ImagePadding -> {{10, 10}, {10, 10}}]

enter image description here

The gridlines run through the centers of the "meshpoints" and are uniformely spaced at

{2.6, 6.7, 10.8, 14.9, 19.0} // Differences

{4.1, 4.1, 4.1, 4.1}

share|improve this answer
    
how did you find the gridlines' XX coordinates? (trying to adapt this to my problem) – Sosi Nov 10 '15 at 17:32
    
By trial and error - but didn't take too long – eldo Nov 10 '15 at 17:34
    
:P I now remembered there's the "Get coordinates" thing where you can see them quickly! – Sosi Nov 10 '15 at 17:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.