Announcement

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

  • Graph Hbar by Groups

    Hello all. Currently, my goal is to have the bar graph code below to color code the observations below based on the variable GROUP and to have a legend associated with the variable GROUP with its associated colors. Below is a data example:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 NAME float TIMEHRS str12 GROUP
    "John"   .5 "Experienced"
    "Dilly" 1.1 "Intermediate"
    "Bob"   1.7 "Beginner"    
    "Josh"  1.5 "Beginner"    
    end
    The code below generates a horizontal bar chart. I'd like for the observations to be colored based on the variable GROUP and to have an associated legend plot.

    Code:
    graph hbar TIMEHRS, over(NAME) bar(1, color(blue)) l1title("Subjects") b1title("Time to complete task") ///
    ytitle(, size(tiny)) subtitle("Subjects completing task (N=4)")
    Unfortunately, I haven't found out how to accomplish this using by() and another over() option. Thank you for your time.

  • #2
    Thanks for the data example.

    Code:
    graph hbar TIMEHRS, over(GROUP) over(NAME) bar(1, color(blue)) bar(2, color(red)) bar(3, color(gray)) ///
     l1title("Subjects") b1title("Time to complete task") ytitle(, size(tiny))  ///
    subtitle("Subjects completing task (N=4)") asyvars nofill
    You may also consider the following that does away with the legend.

    Code:
    graph hbar TIMEHRS, over(NAME) over(GROUP) bar(1, color(blue)) l1title("Subjects") ///
    b1title("Time to complete task") ytitle(, size(tiny)) subtitle("Subjects completing task (N=4)") nofill

    Comment


    • #3
      You can try using asyvars as an extra option, This works better in my experience:


      Code:
      clear
      input str5 NAME float TIMEHRS str12 GROUP
      "John"   .5 "Experienced"
      "Dilly" 1.1 "Intermediate"
      "Bob"   1.7 "Beginner"    
      "Josh"  1.5 "Beginner"    
      end
      
      separate TIMEHRS, by(NAME) veryshortlabel 
      graph hbar (asis) TIMEHRS?, over(NAME) nofill bar(1, color(blue)) l1title("Subjects") b1title("Time to complete task") ///
      ytitle(, size(tiny)) subtitle("Subjects completing task (N=4)") legend(off)
      Click image for larger version

Name:	bar4.png
Views:	1
Size:	20.2 KB
ID:	1466123



      That said, this fruit salad or technicolour dreamcoat effect can be distracting. Do you really need it? (Highlighting one bar among several can be extremely effective, I do admit.)

      Comment


      • #4
        Thank you all for your help. Andrew's code was in line of what I needed but I will keep in mind what you've brought up Nick.

        Comment

        Working...
        X