Announcement

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

  • Clustered Bar Chart

    I have two variables "group" and "agegrp3". Both are categorical variables with 2 and 3 categories, respectively. I want to make a clustered bar chart as shown in the attached figure, but I want different colors (green, red, and blue) for the three bars representing the 3 categories of agegrp3. Here is the data example.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int caseid byte(agegrp3 group)
     1 1 0
     2 1 0
     3 1 0
     4 2 0
     5 0 0
     6 1 0
     7 0 0
     8 1 0
     9 1 0
    10 2 0
    11 1 0
    12 1 0
    13 1 0
    14 0 0
    15 1 0
    16 0 0
    17 1 0
    18 1 0
    19 2 0
    20 0 0
    21 2 0
    22 0 0
    24 1 0
    25 2 0
    26 0 0
    27 2 0
    28 0 0
    29 2 0
    30 0 0
    31 2 0
    32 0 0
    33 1 0
    34 1 0
    35 2 0
    36 0 0
    38 2 0
    39 1 0
    40 1 0
    41 1 0
    42 2 0
    43 1 0
    44 2 0
    45 0 0
    46 0 0
    47 1 0
    48 0 0
     1 1 1
     2 0 1
     3 2 1
     4 1 1
     5 2 1
     6 1 1
     7 2 1
     8 1 1
     9 0 1
    11 2 1
    12 0 1
    13 2 1
    14 0 1
    17 2 1
    18 2 1
    19 1 1
    20 1 1
    21 2 1
    22 0 1
    23 1 1
    24 1 1
    25 1 1
    26 0 1
    27 0 1
    28 1 1
    29 2 1
    30 2 1
    31 0 1
    32 0 1
    33 2 1
    34 0 1
    35 1 1
    36 1 1
    37 0 1
    38 1 1
    39 2 1
    40 2 1
    41 0 1
    42 0 1
    43 0 1
    44 1 1
    45 1 1
    46 2 1
    47 0 1
    48 2 1
    49 1 1
    50 0 1
    51 0 1
    52 1 1
    53 2 1
    54 1 1
    55 1 1
    57 1 1
    58 0 1
    end
    label values agegrp3 agegrp3
    label def agegrp3 0 "10-29", modify
    label def agegrp3 1 "30-49", modify
    label def agegrp3 2 ">=50", modify
    label values group group
    label def group 0 "control", modify
    label def group 1 "case", modify
    Here is the code I used to make the bar chart attched.
    Code:
    graph bar (count), over(agegrp3, gap(1)) over(group) ytitle("Frequency")
    Attached Files

  • #2
    Code:
    graph bar (count), over(agegrp3, gap(1)) over(group) ytitle("Frequency") asyvars showyvars leg(off)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	12.8 KB
ID:	1762944

    Comment


    • #3
      Thank you very much Andrew Musau ... Do we have an option to set some different colors for the three bars? Like, If I want them to be, say, olive, purple, and magenta how do I do that?

      Comment


      • #4
        Add to the options:

        Code:
        bar(1, color(olive)) bar(2, color(purple)) bar(3, color(magenta))

        Comment


        • #5
          To find out how to modify Andrew Musau's code go to

          Code:
          help graph bar
          and look for barlook_options.

          Here is a different take. I used tabplot from the Stata Journal.

          Note my edits to the value labels and the hybrid graph and table form. If you wanted bars proportional to frequencies, not percents, that is an easy fix. Naturally you could have different colours, including none at all.


          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int caseid byte(agegrp3 group)
           1 1 0
           2 1 0
           3 1 0
           4 2 0
           5 0 0
           6 1 0
           7 0 0
           8 1 0
           9 1 0
          10 2 0
          11 1 0
          12 1 0
          13 1 0
          14 0 0
          15 1 0
          16 0 0
          17 1 0
          18 1 0
          19 2 0
          20 0 0
          21 2 0
          22 0 0
          24 1 0
          25 2 0
          26 0 0
          27 2 0
          28 0 0
          29 2 0
          30 0 0
          31 2 0
          32 0 0
          33 1 0
          34 1 0
          35 2 0
          36 0 0
          38 2 0
          39 1 0
          40 1 0
          41 1 0
          42 2 0
          43 1 0
          44 2 0
          45 0 0
          46 0 0
          47 1 0
          48 0 0
           1 1 1
           2 0 1
           3 2 1
           4 1 1
           5 2 1
           6 1 1
           7 2 1
           8 1 1
           9 0 1
          11 2 1
          12 0 1
          13 2 1
          14 0 1
          17 2 1
          18 2 1
          19 1 1
          20 1 1
          21 2 1
          22 0 1
          23 1 1
          24 1 1
          25 1 1
          26 0 1
          27 0 1
          28 1 1
          29 2 1
          30 2 1
          31 0 1
          32 0 1
          33 2 1
          34 0 1
          35 1 1
          36 1 1
          37 0 1
          38 1 1
          39 2 1
          40 2 1
          41 0 1
          42 0 1
          43 0 1
          44 1 1
          45 1 1
          46 2 1
          47 0 1
          48 2 1
          49 1 1
          50 0 1
          51 0 1
          52 1 1
          53 2 1
          54 1 1
          55 1 1
          57 1 1
          58 0 1
          end
          label values agegrp3 agegrp3
          label def agegrp3 0 "10-29", modify
          label def agegrp3 1 "30-49", modify
          label def agegrp3 2 "{&ge}50", modify
          label values group group
          label def group 0 "Control", modify
          label def group 1 "Case", modify
          
          bysort group (agegrp3) : gen total = _N 
          by group agegrp3 : gen freq = _N 
          gen pc = 100 * freq / total 
          gen toshow = strofreal(freq) + " (" + strofreal(pc, "%2.1f") + "%)"
          
          set scheme stcolor 
          tabplot agegrp3 group , showval(toshow) yreverse percent(group) ytitle(Age group) xtitle("") subtitle(bars show % breakdown) aspect(1) separate(agegrp3)
          Click image for larger version

Name:	agecontrolcase.png
Views:	1
Size:	25.4 KB
ID:	1762953

          Comment

          Working...
          X