Announcement

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

  • Different colors for two groups of bars in a bar chart

    Dear all,
    I am trying to make a bar chart that shows mean value of a variable (attitudes toward immigrants) for 19 European countries. I want to use different colors (or intensities of same color) for East European (ex-communist) countries and West European countries. I have created a dummy variable that distinguishes between these two categories of countries, but was not able to find any info about how i can make the graph.
    Syntax is bellow and an example of the graph using only one color for the bars is attached.

    Any help is much appreciated.
    Best,
    Zan Strabac

    graph bar (mean) immigr_att [pw=pspwght], over(country, sort(1) lab(angle(45))) intensity (50)


    Attached Files

  • #2
    Please give a data example and show your existing graph as .png not .gph. FAQ Advice #12 explains both points.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Please give a data example and show your existing graph as .png not .gph. FAQ Advice #12 explains both points.
      Ok, trying to post in accordance with FAQ #12:

      Code:
       graph bar (mean) immigr_att [pw=pspwght], over(country, sort(1) lab(angle(45))) intensity (50)
      Presenting a short random sample of data, and only 5 countries using randomtag and dataex:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(immigr_att pspwght) long(country excom)
              5 1.2814763 1 1
              4  .4017383 1 1
      4.6666665  .6357259 1 1
             10  .6878853 1 1
      4.3333335  .6646395 1 1
      4.6666665  .9279124 1 1
              7  .7465485 1 1
       6.333333  .9133835 1 1
              .  2.667386 1 1
              .  .6074823 1 1
      1.6666666 1.8672407 1 1
       3.333333  .4003073 1 1
       7.333333   .872346 2 0
              8 1.1248171 2 0
              9 1.0638217 2 0
      2.3333333  .8510109 2 0
       8.666667  .9286219 2 0
              7 1.1564138 2 0
       8.333333 1.0456595 2 0
              .  1.227084 2 0
       3.666667  .9934008 3 1
              2  .5836357 3 1
              6  .9704045 3 1
       5.333333  .6254382 3 1
              .  .6605008 3 1
              0  .6483748 3 1
      1.6666666 1.1796651 3 1
       6.666667   .907792 3 1
              6   .842384 4 1
       3.333333  .7788791 4 1
              7 1.2844403 4 1
       3.333333 1.1847209 4 1
       3.666667  .7790892 4 1
      1.6666666  .9848319 4 1
              5  .7565995 4 1
      2.3333333 1.2162174 4 1
              .  1.331813 4 1
              6  .8426083 4 1
              5  .7302645 4 1
       6.666667 1.2549235 5 0
              7  .7530739 5 0
       7.666667  .7660794 5 0
       5.333333  .6529269 5 0
              8  .6093153 5 0
       7.666667  .7660794 5 0
              7  .5989709 5 0
       5.666667 1.0349259 5 0
       6.333333 1.2931395 5 0
              6  .9268494 5 0
       2.666667 1.3568442 5 0
      end
      label values country cntr_lbl
      label def cntr_lbl 1 "Bulgaria", modify
      label def cntr_lbl 2 "Switzerland", modify
      label def cntr_lbl 3 "Czechia", modify
      label def cntr_lbl 4 "Estonia", modify
      label def cntr_lbl 5 "Finland", modify
      And finally, attaching the original graph (that i would like to createin separate bar colors for East and West Eu. countries) as a .png file:
      Attached Files

      Comment


      • #4
        Thanks for the extra detail. Try

        Code:
        separate immigr_att, by(excom)
        graph bar (mean) immigr_att? [pw=pspwght], over(country, sort(1) lab(angle(45))) intensity(50) nofill legend(order(1 "West European" 2 "East European")) ytitle(Some wording here)
        except that I recommend hbar not least to avoid those 45 degree slopes (steeper than scree or talus!).

        For more detail, see https://www.stata-journal.com/articl...article=gr0049

        https://en.wikipedia.org/wiki/Scree

        Comment


        • #5
          Thanks for the code Nick, but I was trying to use different colors for East and West, while preserving the ordering by the dependent variable (sorry for not mentioning explicitly in the first post). So, basically a version of what have been done for Missisippi in the River dataset in your article in Stata Journal. I have tried to adapt the code from the article, but was not able to make it work.

          Comment


          • #6
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(immigr_att pspwght) long(country excom)
                    5 1.2814763 1 1
                    4  .4017383 1 1
            4.6666665  .6357259 1 1
                   10  .6878853 1 1
            4.3333335  .6646395 1 1
            4.6666665  .9279124 1 1
                    7  .7465485 1 1
             6.333333  .9133835 1 1
                    .  2.667386 1 1
                    .  .6074823 1 1
            1.6666666 1.8672407 1 1
             3.333333  .4003073 1 1
             7.333333   .872346 2 0
                    8 1.1248171 2 0
                    9 1.0638217 2 0
            2.3333333  .8510109 2 0
             8.666667  .9286219 2 0
                    7 1.1564138 2 0
             8.333333 1.0456595 2 0
                    .  1.227084 2 0
             3.666667  .9934008 3 1
                    2  .5836357 3 1
                    6  .9704045 3 1
             5.333333  .6254382 3 1
                    .  .6605008 3 1
                    0  .6483748 3 1
            1.6666666 1.1796651 3 1
             6.666667   .907792 3 1
                    6   .842384 4 1
             3.333333  .7788791 4 1
                    7 1.2844403 4 1
             3.333333 1.1847209 4 1
             3.666667  .7790892 4 1
            1.6666666  .9848319 4 1
                    5  .7565995 4 1
            2.3333333 1.2162174 4 1
                    .  1.331813 4 1
                    6  .8426083 4 1
                    5  .7302645 4 1
             6.666667 1.2549235 5 0
                    7  .7530739 5 0
             7.666667  .7660794 5 0
             5.333333  .6529269 5 0
                    8  .6093153 5 0
             7.666667  .7660794 5 0
                    7  .5989709 5 0
             5.666667 1.0349259 5 0
             6.333333 1.2931395 5 0
                    6  .9268494 5 0
             2.666667 1.3568442 5 0
            end
            label values country cntr_lbl
            label def cntr_lbl 1 "Bulgaria", modify
            label def cntr_lbl 2 "Switzerland", modify
            label def cntr_lbl 3 "Czechia", modify
            label def cntr_lbl 4 "Estonia", modify
            label def cntr_lbl 5 "Finland", modify
            
            separate immigr_att, by(excom)
            graph bar (mean) immigr_att? [pw=pspwght], over(country, sort(immigr_att) lab(angle(45))) intensity(50) nofill legend(order(1 "West European" 2 "East European")) ytitle(Some wording here) scheme(s1color)
            Last edited by Nick Cox; 08 Mar 2023, 11:47.

            Comment


            • #7
              This did the deed, thank you so much, Nick. (And thanx for proposing different colors, using Stata's default blue and red might have some unwanted connotations in this case :-))
              I am posting the final graph for the benefit of possible future readers of this tread:

              Attached Files

              Comment

              Working...
              X