Announcement

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

  • Labeling Graphs after "Separate" and "Collapse"

    Hello,
    I have the following code:

    Code:
        preserve
        separate income, by(gender)
        collapse(median) income1 income2 income3 [weight=weight], by(year education)
        graph twoway line income1 income2 income3 year, by(education) yla(, ang(h)) ytitle("Income (in USD)") title("Income by Gender")
        restore
    It works well, except the variable names "income1, income2, income3" appear in the legend. I don't want to use the variable labels either (e.g. "income, gender == other"). It seems like without the preserve command, I could have just manually renamed the variables that "separate" generates. However, I'd want the label to be "woman, men, other" rather than "income of women" etc., but this would only make sense for the graph and not the rest of the data. So what's the easiest way for me to change the legend labels?

    In case it matters, I'd like to run the same code again with graphs by gender rather than education as well.

    Also, how do I change the title so that it titles all 3 graphs generated once, rather than all 3 individually?

    I hope this makes sense! Thank you in advance

  • #2
    Edit: I found "legend(label(1 "woman") label(2 "man"), which works. Still looking how to title graph window, and how to have the year on the x-axis of all of the graphs

    Comment


    • #3
      This should answer your first question:

      Code:
      set scheme s1color
      // open example data
      sysuse nlsw88, clear
      
      // prepare the data
      gen byte occat = cond(occupation < 3, 1,                    ///
                       cond(inlist(occupation,5, 6, 8, 9), 2, 3)) ///
                       if !missing(occupation)
      label variable occat "occupational category"
      label define occat 1 "white collar" ///
                         2 "skilled"      ///
                         3 "unskilled"
      label value occat occat
      gen byte urban = c_city + smsa
      label define urban 2 "central city" ///
                         1 "suburban"     ///
                         0 "rural"
      label value urban urban
      label variable urban "urbanicity"
      
      // collapse and separate
      collapse (median) wage , by(occat grade urban)
      separate wage, by(urban) veryshortlabel
      
      // graph
      graph twoway line wage? grade, by(occat, note("") ) ytitle(median hourly wage)
      I don't understand your last question
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Add the ixaxes sub-option inside the by() option to get an x-axis on all the sub-graphs
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Hi Maarten,

          Thank you for your response! I'm looking into ixaxes now. For the other question, if I have graph twoway line..., title("This is my Title"), it applies this title to all the graphs in the window individually. However, I'd like to have one overarching title for all the graphs. Does that make sense. Thank you!

          Comment


          • #6
            you have to put the title() option inside the by() option
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Maarten Buis Could you please advise on how to include the weights additionally when creating the two-way line graph? And which weights should be applied? (aw or else)?
              Last edited by Hend She; 10 Nov 2021, 05:32.

              Comment

              Working...
              X