[Solved] Bar chart with two bar colors - positives green, negatives red
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.
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.
- Create a new variable named Score_is_positive
- 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.
- 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.
- While viewing the graph of score, set your Vertical axis = Person, Key = Score_is_positive
- Right-click on a bar (or key item) / Change series color... Select the green and red colors you would like to use. [OK]
- (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.
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).
Bars appear in the table. Drag the column to make it wider to get this:
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: