Bar chart with two ...

Clear all

# [Solved] Bar chart with two bar colors - positives green, negatives red

Posts: 9
Customer
Topic starter
Eminent Member
Joined: 1 year ago

How would you create a bar chart where the positive values have green bars, the negative values have red bars?

The attached model file shows what I have in mind (sorry, I don't see a way to paste an image here).

I don't want static colors - I want the colors to adapt to the values.

Topic Tags
4 Replies
Posts: 18
Moderator
Member
Joined: 1 year ago

In the jargon of Analytica graphing, you would say "I want the Color (role) to depict the sign of Score".  So, since you need to depict sign-of-score, you'll need to make that available.

1. Create a new variable named Score_is_positive
2. Define it as Score > 0
Note: You could use Sign(Score), but you'd have 3 possible values. Here it is easier to have just 2.
3. When viewing the result for your main result, Score, in edit mode, press the [XY] button / Use another variable / Add... / Score_is_positive [OK]
Now, you have the option of depicting it on your graph.
4. While viewing the graph of score, set your Vertical axis = Person, Key = Score_is_positive
5. Right-click on a bar (or key item) / Change series color...  Select the green and red colors you would like to use. [OK]
6. (Optional) Turn off the key. Either in graph setup, style tab, or in Analytica 6.0 you can select "Hide this key" on the right-mouse menu for the key.

With that, you have your graph (and unlike your excel graph, the people are actually in the correct order!).

The above steps come pretty naturally if you just get use to saying "I want the Color role to depict ???".  So then, you just need to make sure you have ??? available, often as its own variable.

Finally, to make this really solid, there is one additional step you should do, which is to set the domain of Score_is_positive. The domain tells Analytica what the possible values are. (Here the possible values are 0 and 1) In this case, you can select Domain = Boolean, or you could use explicit values [0,1]. If you skip this step and later ended up with no negative values -- only positives -- your positive bars would be red.  Why? Because the first value (and only value) that appears is 1, hence the first color in the color series is used, which is red. When the domain is set, this determines the ordering for values, so score_is_positive is the 2nd item in the color series, even if a 0 never appears.

Joined: 12 years ago

Member
Posts: 22

Here's a screenshot after following your instructions (without step 6).

Posts: 22
Member
Joined: 12 years ago

Here is another option, to get bars in table cells (rather than in the graph). Starting with the original variable Score in Pat's model, view the result Table (instead of result graph).

Now, go to the Object window and edit the Cell Format Expression attribute (you may need to make it visible first). Enter this expression for the Cell Format Expression:

CellBar(self,'green',negColor:'red')

Bars appear in the table.  Drag the column to make it wider to get this:

Posts: 18
Moderator
Member
Joined: 1 year ago

How about one further embellishment -- negative values left aligned:

The Cell format expression for this is

CellFormats(
CellBar(self,'green',negColor:'red'),
if self<0 then CellAlignment('Left')
)

Note: Use CellFormats( ) when you want to combine multiple cell formats. See also:

Share:
Scroll to Top