Announcement

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

  • Legend graph

    Hello,

    I would like to add legends for each of the lines that I plot in a graph. It always appears as error saying "parentheses do not balance" The code that I'm using is:

    twoway (scatter mean_gdp_growth1 year, c(1) yaxis(1)) (scatter mean_gdp_growth2 year, c(1) yaxis(2)) (scatter mean_gdp_growth3 year, c(1) yaxis(2)) ///
    (scatter mean_gdp_growth4 year, c(1) yaxis(2)), title (GDP growth), legend (label(1 "Scandinavian") (label(2 "Continental") (label(3 "Anglo-Saxon") (label4 "Mediterranean")

    Can you help me to identify the error?
    Thank you in advance

  • #2
    I think you have several small errors there. Nothing we can test directly given no data example but

    1. When we ask that you use CODE delimiters to show code readably, we do mean what we say. https://www.statalist.org/forums/help#stata applies.

    2. When a graph command gets too long (for me to understand) I shift the code to a do-file editor window and space it out on several physical lines and use continuation characters as well.

    3. As a matter of taste but also practicality I never use () to separate graph commands. My rationale: there are enough parentheses already, and on top of that it's harder to spot matches or mismatches.

    4. You have at least one comma too many in between the title() option and the legend() option.

    5. It is not a good idea to put spaces between option names and their arguments.

    I got to this.


    Code:
    scatter mean_gdp_growth1 year, c(1)    yaxis(1) ///
    || scatter mean_gdp_growth2 year, c(1) yaxis(2) ///
    || scatter mean_gdp_growth3 year, c(1) yaxis(2) ///
    || scatter mean_gdp_growth4 year, c(1) yaxis(2) ///
    title(GDP growth) legend(order(1 "Scandinavian" 2 "Continental" 3 "Anglo-Saxon" 4 "Mediterranean"))
    Now it is easier to think about.

    6. Next I think you can do this. Variables 2, 3 and 4 can go in one command.

    Code:
    twoway connect mean_gdp_growth1 year,   yaxis(1) ///
    || connect mean_gdp_growth2  mean_gdp_growth3 mean_gdp_growth4 year, yaxis(2) ///
    title(GDP growth) legend(order(1 "Scandinavian" 2 "Continental" 3 "Anglo-Saxon" 4 "Mediterranean"))
    7. Note that I cut out c(1) which is just a typo for c(l). I didn't spot that in the original although it is now obvious when you know what to look for.
    Last edited by Nick Cox; 25 Apr 2020, 04:03.

    Comment


    • #3
      Thank you very much Nick Cox. It worked

      Comment

      Working...
      X