Announcement

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

  • graph bar with labels instead of legend

    Hi all,

    I'm investigating students' reasons for choosing their study. Toydata example:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(reason1 reason2 reason3 reason4)
    1 1 1 1
    . 1 1 .
    . . 1 .
    1 1 1 1
    1 . 1 .
    1 . 1 1
    1 1 1 1
    . . . .
    . . 1 .
    end
    As you can see, students can choose multiple reasons. I want to visualise the frequency of each reason, which I'm trying to do as follows:

    Code:
    graph bar (count) reason1 reason2 reason3 reason4
    However, I want to remove the legend and instead have the variables' names written under each bar. I know I can simply use
    Code:
    , legend(off)
    , but I have no idea how to get the variables' names. Any ideas?

  • #2
    Code:
    graph bar (count) reason1 reason2 reason3 reason4, asyvars showyvars leg(off)
    but you will typically want to change the default labels.

    Comment


    • #3
      Here is some technique that may help. Getting informative text is an even deeper incentive than getting variable names.

      Code:
      clear
      input byte(reason1 reason2 reason3 reason4)
      1 1 1 1
      . 1 1 .
      . . 1 .
      1 1 1 1
      1 . 1 .
      1 . 1 1
      1 1 1 1
      . . . .
      . . 1 .
      end
      
      set scheme s1color 
      
      forval j = 1/4 {
          egen total`j' = total(reason`j')
          label var total`j' "`j'"
      }
      
      graph hbar (asis) total* in 1, ascategory l1title(Reason) ytitle(Frequency) ysc(alt)
      Click image for larger version

Name:	totalsbar.png
Views:	1
Size:	13.2 KB
ID:	1710459

      Comment


      • #4
        Not what you asked, but may be interesting or useful any way. See upsetplot (SSC) for a bar chart based on showing an Euler-Venn diagram but with frequencies. This thus shows the joint frequency distribution, not just a collapse to univariate counts. The first plot below shows subsets in decreasing frequency order; the second exemplifies the scope for different sort orders.

        See https://www.statalist.org/forums/for...lable-from-ssc for more.

        Code:
        clear
        input byte(reason1 reason2 reason3 reason4)
        1 1 1 1
        . 1 1 .
        . . 1 .
        1 1 1 1
        1 . 1 .
        1 . 1 1
        1 1 1 1
        . . . .
        . . 1 .
        end
        
        mvencode reason*, mv(0)
        
        forval j = 1/4 {
            label var reason`j' "`j'"
        }
        
        upsetplot reason*, variablelabels name(G1, replace)
        
        upsetplot reason*, variablelabels name(G2, replace) gsort(-_degree -_count)


        Comment


        • #5
          Originally posted by Andrew Musau View Post
          Code:
          graph bar (count) reason1 reason2 reason3 reason4, asyvars showyvars leg(off)
          but you will typically want to change the default labels.
          Thanks
          Last edited by Sara Hansen; 20 Apr 2023, 01:34.

          Comment

          Working...
          X