Announcement

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

  • How to put labels in mutiple twoway line graphs

    Hi, I would like to draw multiple lines in one graph like the following using the data below. I have 9 y variables, so even if I use different line patterns (using lpattern option), Stata graph output does not distinguish them very well, especially in a black and white printout. This problem persists even if I use different thickness option for each line. Is there any way I can put a label (y1 - y9) in each line inside the plotspace instead of legend outside? Thanks in advance.

    twoway (line y1 x) (line y2 x) (line y2 x) (line y3 x) (line y4 x) (line y5 x) (line y6 x) (line y7 x) (line y8 x) (line y9 x)
    x y1 y2 y3 y4 y5 y6 y7 y8 y9
    -1 0.50 0.26 0.39 0.30 0.38 0.70 0.01 0.00 0.23
    -0.8 0.47 0.26 0.40 0.29 0.39 0.66 0.02 0.00 0.22
    -0.6 0.45 0.26 0.41 0.28 0.40 0.63 0.02 0.00 0.21
    -0.4 0.42 0.26 0.42 0.26 0.41 0.60 0.03 0.00 0.19
    -0.2 0.40 0.26 0.43 0.25 0.42 0.57 0.03 0.00 0.18
    0 0.37 0.26 0.44 0.23 0.42 0.54 0.04 0.00 0.17
    0.2 0.35 0.26 0.44 0.22 0.42 0.52 0.05 0.00 0.16
    0.4 0.33 0.26 0.45 0.20 0.43 0.49 0.06 0.00 0.15
    0.6 0.30 0.26 0.46 0.19 0.43 0.47 0.08 0.00 0.14
    0.8 0.28 0.26 0.47 0.17 0.42 0.44 0.10 0.01 0.13
    1 0.26 0.26 0.48 0.16 0.42 0.42 0.12 0.01 0.13

  • #2
    Ray,

    There's quite a lot going on in that graphic. I wonder if there is some meaningful way to separate the different dependent variables and use at least two, maybe more different plots. Even with labels directly in the plot region, it may be difficult for readers to make sense of the dense data presentation. Anyway, here's my thoughts on the technical aspect of what you're asking.

    Others may understand the inner workings of the graph commands better than me and have a way of calling the labels without having to figure out which lines go with which labels like I have in the example here.

    Code:
    clear
    input float(x y1 y2 y3 y4 y5 y6 y7 y8 y9)
     -1  .5 .26 .39  .3 .38  .7 .01   0 .23
    -.8 .47 .26  .4 .29 .39 .66 .02   0 .22
    -.6 .45 .26 .41 .28  .4 .63 .02   0 .21
    -.4 .42 .26 .42 .26 .41  .6 .03   0 .19
    -.2  .4 .26 .43 .25 .42 .57 .03   0 .18
      0 .37 .26 .44 .23 .42 .54 .04   0 .17
     .2 .35 .26 .44 .22 .42 .52 .05   0 .16
     .4 .33 .26 .45  .2 .43 .49 .06   0 .15
     .6  .3 .26 .46 .19 .43 .47 .08   0 .14
     .8 .28 .26 .47 .17 .42 .44  .1 .01 .13
      1 .26 .26 .48 .16 .42 .42 .12 .01 .13
    end
    
    twoway line y1 y2 y3 y4 y5 y6 y7 y8 y9 x, ///
        text(.7 -1 "y6" .5 -1 "y1") || ///
        pcarrowi .5 -.2 .44 0 "y3", mlabposition(9) || ///
        pcarrowi .38 .4 .42 .2 "y5"

    The text option places the labels directly on the point specified, which in this case is directly on the line. You can use the placement option to move them around or play with the point specified. Notice you can specify multiple labels in the same option. If you want to add arrows, you can repeatedly use the pcarrowi plot.

    Comment


    • #3
      You are looking for legend options help legend_options for more details
      Code:
      tw line y* x, legend(lab (1 "YY") lab(2 "ZZ") lab(3 "TT"))

      Comment

      Working...
      X