Skip to content
Bar chart with two ...
 
Notifications
Clear all

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

6 Posts
4 Users
0 Reactions
3,377 Views
Avatar
Posts: 10
Customer
Topic starter
(@patstevens)
Eminent Member
Joined: 3 years 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.

5 Replies
Avatar
Posts: 26
Moderator
(@drice)
Member
Joined: 3 years 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.

Reply
1 Reply
Lonnie Chrisman
Admin
(@lchrisman)
Joined: 14 years ago

Member
Posts: 42

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

Reply
Lonnie Chrisman
Posts: 42
Admin
(@lchrisman)
Member
Joined: 14 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:

Reply
Avatar
Posts: 26
Moderator
(@drice)
Member
Joined: 3 years 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:

 

Reply
Avatar
Posts: 3
Guest
(@Anonymous 10794)
New Member
Joined: 1 year ago

Hallo, your information makes me learn a lot. Thanks

Reply
Share:

Download the free edition of Analytica

The free version of Analytica lets you create and edit models with up to 101 variables, which is pretty substantial since each variable can be a multidimensional array. It also lets you run larger modes in ‘browse mode.’ Learn more about the free edition.

While Analytica doesn’t run on macOS, it does work with Parallels or VMWare through Windows.


    Analytica Cubes Pattern

    Download the free edition of Analytica

    The free version of Analytica lets you create and edit models with up to 101 variables, which is pretty substantial since each variable can be a multidimensional array. It also lets you run larger modes in ‘browse mode.’ Learn more about the free edition.

    While Analytica doesn’t run on macOS, it does work with Parallels or VMWare through Windows.


      Analytica Cubes Pattern