Announcement

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

  • Use of legends with by()

    Dear Stata users,

    I have a question about the use of legends with by() option. In the following code, I put legend(pos(6) row(1)) inside the by() option, and not surprisingly (as it was described in the graph help file 'help legend_options##use_of_legends_with_by', and I quote it here), the row(1) suboption was ignored. And I must admit that I read that help file, however, I do not understand what it really means. Would anyone teach me how to place legends in 1 row?
    ... you wish to move the legend, you must distinguish between legend(contents) and legend(location).
    ... The former must appear outside the by(). The latter appears inside the by(). legend(col(1)) was placed in the command just where we would place it had we not specified by() but that legend(pos(4)) was moved to be inside the by() option. We did that because the cols() suboption is documented under contents in the syntax diagram, whereas position() is documented under location. The logic is that, at the time the individual plots are constructed, they must know what style of key they are producing. The placement of the key, however, is something that happens when the overall graph is assembled, so you must indicate to by() where the key is to be placed.
    Code:
    sysuse auto
    xtile weight5=weight, n(5)
    label define weight5 1 "1760-2160" 2 "2200-2730" 3 "2750-3310" 4 "3330-3700" 5 "3720-4840"
    label values weight5 weight5
    graph pie price, over(weight5) by(foreign, legend(pos(6) row(1))) plabel(_all sum, format(%9.0f))
    Click image for larger version

Name:	Graph1.png
Views:	1
Size:	177.4 KB
ID:	1739767
    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	155.7 KB
ID:	1739768

    Last edited by Chen Samulsion; 14 Jan 2024, 09:52.

  • #2
    inside by() -- outside by()

    Code:
    by(... legend(pos(...))) legend(row(...))

    Comment


    • #3
      Dear Andrew Musau thank you very much. That's great!

      Comment

      Working...
      X