Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Twoway bar chart! Different colors for different groups

    Hello everyone, I sorted a list by groups and made a graph which looks like this. Now I want to color each group differently and put a name on top of each group. The first bars belong to one group, the next bars to another group and so on. Does anyone has any idea how to do that? I used the following command to create the graph: twoway bar sign_eg2_neu rank_neu, base(0) bfcolor(none)
    I also tried out many different commands, but nothing worked. In the end the bars should look like the last picture.

    Thank you very much for your help guys!!!!



    Click image for larger version

Name:	Unbenannt1.png
Views:	1
Size:	13.8 KB
ID:	1359579
    Click image for larger version

Name:	Unbenannt.png
Views:	1
Size:	8.0 KB
ID:	1359578

    Click image for larger version

Name:	3.png
Views:	1
Size:	40.4 KB
ID:	1359580

  • #2
    Chris,

    One way is to treat each group as a different plot and overlay each group's plot onto the same graph. Here's an example of what I mean...

    twoway ///
    bar sign_eg2_neu rank_neu if Group==1, base(0) || ///
    bar sign_eg2_neu rank_neu if Group==1, base(0) || ///
    bar sign_eg2_neu rank_neu if Group==1, base(0) || ///
    bar sign_eg2_neu rank_neu if Group==1, base(0) ///
    legend(off) text(.1 7 "TOU" .1 19 "PTR")

    From there you can change the color of the bars if you don't like them.

    Adding text is as easy as adding a -text- option. In the code above, the text option will add the text at the y and x coordinates given (in that order). I always have to play around a little before I get the placement right.

    By the way, I haven't texted the code.

    HTH,
    Lance

    Comment


    • #3
      We can't see your code or your data (that we can copy and paste and relate to your graph). Please do read the FAQ Advice, especially #12, as the prompt for each new message does request.


      Code:
      clear
      sysuse auto
      set scheme s1color
      sort rep78 mpg
      gen rank = _n
      separate rank, by(rep78) veryshortlabel
      
      tokenize "blue orange black red cyan"
      
      forval j = 1/5 {
          local call `call' || bar mpg rank if rep78 == `j', bcolor(``j'')
      }
      
      twoway `call' base(0) legend(order(1 "1" 2 "2" 3 "3" 4 "4" 5 "5") row(1)) ///
      xla(1 10(10)70)
      Click image for larger version

Name:	anotheranotherbar.png
Views:	1
Size:	26.6 KB
ID:	1359587


      Note: this is similar in spirit to Lance's solution. In his case, the groups should be in turn 1 2 3 4 5, not 1 repeatedly.

      Comment


      • #4
        Yes, I forgot to change the group number after copying and pasting. Sorry.

        Comment


        • #5
          Thanks again Mr. Cox for your quick answer, I tried out your version and it worked very well! Haven't tried your versions, Lance, I'll do that tomorrow. Thanks anyways for your help

          Comment

          Working...
          X