Announcement

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

  • Line graph by group: labeling legend

    I am making a line plot of Average test scores against Year for several countries (Jurisdictions) in Latin America. The following command, when applied to the attached data, gets me almost what I want, with each line rendered somewhat differently (solid, broken, dotted, etc.). The only problem is that the legend labels each line symbol as "Average" instead of labeling it with the name of the Jurisdiction. How can I get the lines labeled with the names of the Jurisdiction? Many thanks.

    clear
    import excel "C:\Users\pvonhippel\Box Sync\Chile\PISA trends, IDEExcelExport-Oct052017-0229PM.xls", firstrow sheet("To import")
    destring Average Year, replace
    twoway (line Average Year if Jurisdiction=="Brazil") ///
    (line Average Year if Jurisdiction=="Chile") ///
    (line Average Year if Jurisdiction=="Colombia") ///
    (line Average Year if Jurisdiction=="Costa Rica") ///
    (line Average Year if Jurisdiction=="Mexico") ///
    (line Average Year if Jurisdiction=="Panama") ///
    (line Average Year if Jurisdiction=="Peru") ///
    (line Average Year if Jurisdiction=="Uruguay")


    Attached Files

  • #2
    How about -separate Average, by(Jurisdiction)- , relabelling the variables created if need be, and then plotting referring to the new variables rather than using a set of -if- qualifiers? [Untested; no access to Stata right now]

    Comment


    • #3
      Following Stephen's suggestion, see also http://www.stata-journal.com/sjpdf.h...iclenum=gr0023 for the veryshortlabel option of separate.

      Comment


      • #4
        This is the graph I would make for such data:

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	147.8 KB
ID:	1413510


        Code:
        // load and prepare data
        clear
        import excel "C:\temp\PISA trends, IDEExcelExport-Oct052017-0229PM.xls", firstrow sheet("To import")
        destring Average Year, replace
        drop if Average == .
        
        // add every combination of Jurisdiction and year
        fillin Jurisdiction Year
        
        // sort countries by their average score
        bys Jurisdiction : egen mean = mean(Average)
        egen order = rank(mean), track
        labmask order , values(Jurisdiction)
        
        // add background lines
        tempfile temp
        save `temp'
        
        separate Average, by(Jurisdiction) veryshortlabel
        
        forvalues i = 1/9 {
            bys Year (Average`i') : replace Average`i' = Average`i'[1]
            by Year : gen year`i' = Year[1]
        }
        keep if Jurisdiction == "Brazil"
        keep Year Average? year?
        merge 1:m Year using `temp'
        
        local gr ""
        forvalues i = 1/9 {
            local gr `gr' line Average`i' year`i', lpattern(solid) lcolor(gs12) sort ||
        }
        local gr `gr' scatter Average2 year2, msymbol(o) mcolor(gs12) msize(*.5) || // Buenos Aires
        local gr `gr' scatter Average7 year7, msymbol(o) mcolor(gs12) msize(*.5) || // Panama
        
        // final graph
        twoway `gr' ///
               scatter Average Year, by(order, legend(off) compact note("")) ///
               lpattern(solid) lwidth(*1.2) sort connect(l) msymbol(o)       ///
               xlab(2005(5)2015) ytitle(Average score) xtitle(year)
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Maarten's approach of showing each series with the other series as backdrop was discussed in http://www.stata-journal.com/sjpdf.h...iclenum=gr0046

          There are several other examples closer to Paul's problem

          both here on Statalist (subsetplot is on term useful in search)

          and in https://stats.stackexchange.com/ques.../190328#190328 (which contains several references; others are most welcome).

          Meanwhile please note that labmask used in Maarten's code should be downloaded from the Stata Journal site and generally must be downloaded before it can be used.

          Comment

          Working...
          X