2
$\begingroup$

is there any way to change the default color of the joined lines in BarChart function? As the joined function turns on (i.e., Joined -> True), the joined lines are plotted automatically with the colors of the bar chart. I want to use other colors to show these reference lines. Many thanks for your help!

Here is a toy example in the Mathematica manual.

BarChart[RandomReal[1, {5, 5}], ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5]

enter image description here

$\endgroup$
6
  • 2
    $\begingroup$ BarChart[RandomReal[1, {5, 5}], ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5] /. Line[a_] :> {Cyan, Line[a]} ? $\endgroup$
    – cvgmt
    Jul 18, 2021 at 3:31
  • 1
    $\begingroup$ @cvgmt Thank you very much for your quick reply. Your solution works. But this code also changes the color of the x axis. Is there any way to prevent the color of the axis being changed simultaneously? $\endgroup$
    – Frankie
    Jul 18, 2021 at 3:54
  • $\begingroup$ It's a fragile hack though. It would be strange if there really wasn't any way to change this (without hacks), yet I can't find a way. $\endgroup$
    – Szabolcs
    Jul 18, 2021 at 9:16
  • 1
    $\begingroup$ another hack: BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5] /. Directive[p_PointSize, a_AbsoluteThickness, c_] :> Directive[p, a, Red] $\endgroup$
    – kglr
    Jul 18, 2021 at 12:06
  • 1
    $\begingroup$ yet another: BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5] /. Line[x_] /; Length[x] > 2 :> {Red, Line[x]} $\endgroup$
    – kglr
    Jul 18, 2021 at 12:45

2 Answers 2

2
$\begingroup$
SeedRandom[1]
data = RandomReal[1, {5, 5}];

An alternative hack is to temporarily re-define Darker (which is used to style the joined lines) as the desired line color (say, Red):

Block[{Darker = (Red &)}, 
 BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5]]

enter image description here

To have different colors for each line, post-process to replace Red with desired colors:

linecolors = {Red, Green, Blue, Orange, Magenta};

Block[{Darker = (Red &)}, 
 ReplaceAll[Red :> Last[linecolors = RotateLeft[linecolors]]]@
  BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5]]

enter image description here

$\endgroup$
1
$\begingroup$
SeedRandom[1]
data = RandomReal[1, {5, 5}];

bc = BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5]

enter image description here

Two post-processing hacks:

replaceAll1 = ReplaceAll[Directive[p_, a_, c_] :> Directive[p, a, Red]];

replaceAll2 = ReplaceAll[Line[x_] /; Length[x] > 2 :> {Red, Line @ x}];

replaceAll1 @ bc

enter image description here

replaceAll2 @ bc

same picture

To have a different color for each line:

linecolors = {Red, Green, Blue, Orange, Magenta};

replaceAll1b = ReplaceAll[Directive[p_, a_, c_] :> 
   Directive[p, a, Last[linecolors = RotateLeft[linecolors]]]]

replaceAll2b = ReplaceAll[Line[x_] /; Length[x] > 2 :> 
  {Last[linecolors = RotateLeft[linecolors]], Line @ x}];

replaceAll1b @ bc

enter image description here

replaceAll2b @ bc

same picture

Alternatively, we can use composition of Show and replaceAll1b as the setting for the option DisplayFunction:

displayfunction1 = Show @* replaceAll1b;

BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5, 
   DisplayFunction -> displayfunction1]

enter image description here

Similarly, with replaceAll2b:

displayfunction2 = Show @* replaceAll2b;

BarChart[data, ChartLayout -> "Stacked", Joined -> True, BarSpacing -> 0.5, 
   DisplayFunction -> displayfunction2]

same picture

$\endgroup$

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.