Announcement

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

  • Overall title for a twoway graph with option 'by'

    I'm trying to create a graph using twoway, but I'd only like one title. When I type

    ```sysuse auto
    twoway lpoly price mpg, by(foreign) title("Some cool title here")```

    I get the following:


    But I only want *one* overall title, not the same title repeated for each of the 'by' choices. How can I do this?

  • #2
    Tuck the -title()- option inside the -by()- option.

    Comment


    • #3
      Perfect! I couldn't find anything in the documentation about this. As always, thank you very much Nick!

      Comment


      • #4
        You can find the explanation in [G-3] by_option: http://www.stata.com/help.cgi?by_option#remarks3

        Comment


        • #5
          This solution does not seem to work for -xtline-. May I ask what I can do here?
          Also, can I expand the margin between the plots so they don't collide?
          Code:
          xtline mp_bcrude mp_dcrude if profile!=5, i(profile) t(year) ///
          graphregion(color(white)) ///
          ytitle("Number of births/deaths per 1000 population") ///
          ylabel(10(10)50, angle(360)) ///
          xtitle("Year") xtick(1960(10)2020) ///
          legend(order(1 "Mean CBR" 2 "Mean CDR") ///
          bmargin(medlarge) size(3.5)) ///
          title("Mean Birth and Death Crude Rates (BCR/DCR)", color(black))
          title_xtline.JPG

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int year float(profile mp_bcrude mp_dcrude)
          1960 4  47.57697  23.88866
          1960 2 36.600357 10.586572
          1960 3  43.07193  14.88571
          1960 5  33.43689 12.859654
          1960 1  20.73978  9.758174
          1961 3  43.09346 14.352318
          1961 5  32.90062 12.622182
          1961 1 20.776304  9.605913
          1961 2 35.492573  10.31357
          1961 4  47.50597 23.478643
          1962 5 32.350437 12.483727
          1962 4  47.42825  23.07111
          1962 2 34.880573    10.045
          1962 3  42.80611   13.7254
          1962 1 20.582436  9.890347
          1963 5  31.82678  12.14923
          1963 3  42.90984  13.48659
          1963 2 34.523643  9.802286
          1963 4  47.34671 22.667053
          1963 1  20.62152  9.854783
          1964 2 33.931072  9.529928
          1964 3  42.31495  13.14166
          1964 4  47.26387  22.26734
          1964 1  20.61809  9.568304
          1964 5  31.16189  11.84226
          1965 1  20.05674  9.769566
          1965 2  33.17486  9.264714
          1965 3  41.72713 12.724613
          1965 5  30.44324 11.656273
          1965 4  47.18189 21.871803
          1966 4  47.10225 21.479803
          1966 3   40.6012 12.233088
          1966 5  29.73026 11.431052
          1966 1  19.68304  9.697392
          1966 2 32.145786  9.028571
          1967 2 31.272785  8.750857
          1967 1  19.82461  9.729609
          1967 5  29.15566 11.270287
          1967 3   40.4192 11.989227
          1967 4  47.02484 21.091965
          1968 1  19.43778  9.905261
          1968 4  46.94979  20.70973
          1968 2  30.68586  8.647072
          1968 5  28.61335 11.133958
          1968 3  39.84023  11.65034
          1969 3  39.20823 11.328818
          1969 5  28.21835 11.067592
          1969 2 29.859785    8.3685
          1969 1 18.943956 10.054565
          1969 4  46.87748  20.33584
          1970 3  38.21291 10.942533
          1970 4  46.80923  19.97809
          1970 1     18.37  9.846869
          1970 2  29.60236  8.155714
          1970 5  27.70147 10.916253
          1971 4  46.74625 19.645876
          1971 2 29.400286  7.912571
          1971 1 18.014174   9.92113
          1971 5 27.474916  10.75886
          1971 3  37.91191 10.624434
          1972 1 17.364391  9.790739
          1972 3  37.31251  10.37261
          1972 5  27.39801  10.61995
          1972 2 28.707644    7.7535
          1972 4  46.68741 19.341465
          1973 2  28.09193  7.734929
          1973 1 16.832043  9.825608
          1973 3   36.7228 10.079196
          1973 5  26.90089  10.50464
          1973 4  46.63075 19.061447
          1974 2  27.82743     7.549
          1974 4  46.57388  18.79859
          1974 1 16.840435   9.76513
          1974 5  26.49836 10.402254
          1974 3  36.23951  9.846261
          1975 3 35.703068  9.610847
          1975 1 16.435652     9.827
          1975 5  26.10362 10.303855
          1975 4  46.51707 18.535856
          1975 2 27.027714  7.515143
          1976 3 34.703217  9.398739
          1976 1 16.060217  9.859391
          1976 2  26.69264    7.3595
          1976 5 25.942255  10.17779
          1976 4  46.46062 18.251785
          1977 3  34.26424  9.184717
          1977 4   46.4018 17.931946
          1977 5  25.79578  10.02138
          1977 1 15.695392  9.649522
          1977 2  26.21507  7.229786
          1978 2 25.784143  7.088428
          1978 3 33.951176  8.963087
          1978 4  46.33466 17.570019
          1978 1 15.378695  9.754304
          1978 5 25.619423  9.931966
          1979 3 33.605694  8.736913
          1979 2  25.72993  6.991929
          1979 5 25.698746  9.820933
          1979 1 15.189695  9.691391
          1979 4  46.25114 17.168108
          end
          format %ty year
          label values profile dtgroup
          label def dtgroup 1 "Forerunner", modify
          label def dtgroup 2 "Follower", modify
          label def dtgroup 3 "Trailer", modify
          label def dtgroup 4 "Latecomer", modify
          label def dtgroup 5 "unspecified", modify

          Comment


          • #6
            Hi,
            xtline allows the byopts(...) where you can place the title.
            Hope this helps

            Comment


            • #7
              Solved! Thanks, Nicolai!

              If anyone needs the solution:
              Code:
              xtline mp_bcrude mp_dcrude if profile!=5, i(profile) t(year) ///
              graphregion(color(white)) ///
              ytitle("Number of births/deaths per 1000 population") ///
              ylabel(10(10)50, angle(360)) ///
              xtitle("Year") xtick(1960(10)2020) ///
              legend(order(1 "Mean CBR" 2 "Mean CDR") ///
              bmargin(medlarge) size(3.5)) ///
              byopts(title("Mean Birth and Death Crude Rates (BCR/DCR)", color(black)))

              Comment


              • #8
                I have another problem: How can I cange the lines' colors in the example above?
                All I can change is the first line but I can't find a way to alter the second one as well:
                Code:
                 
                xtline mp_bcrude mp_dcrude if profile!=5, i(profile) t(year) /// graphregion(color(white)) /// ytitle("Number of births/deaths per 1000 population") /// ylabel(10(10)50, angle(360)) /// xtitle("Year") xtick(1960(10)2020) /// legend(order(1 "Mean CBR" 2 "Mean CDR") /// bmargin(medlarge) size(3.5)) /// byopts(title("Mean Birth and Death Crude Rates (BCR/DCR)", color(black))) /// lcolor(black) ///DID NOT WORK
                I also tried -plot#opts(lcolor(black))- -byopts(lc(black))- ;didn't work. Anything helps, thank you!
                Last edited by Rebecca Water; 21 Oct 2018, 17:42. Reason: typo

                Comment


                • #9
                  In the future, please start a new thread if your problem is different to that addressed by the original thread. Make sure to have an appropriate description of your problem for the benefit of those who will search the forum thereafter with a similar problem. Here, complicating the code is not the way to go. Keep it simple

                  Code:
                  xtline mp_bcrude mp_dcrude if profile!=5, i(profile) t(year) ///
                  graphregion(color(white)) ///
                  ytitle("Number of births/deaths per 1000 population") ///
                  ylabel(10(10)50, angle(360)) ///
                  xtitle("Year") xtick(1960(10)2020) ///
                  legend(order(1 "Mean CBR" 2 "Mean CDR") ///
                  bmargin(medlarge) size(3.5)) ///
                  byopts(title("Mean Birth and Death Crude Rates (BCR/DCR)", color(black))) ///
                  lcolor(red green)

                  Comment

                  Working...
                  X