Announcement

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

  • Customizing legend of twoway graph.

    Dear All,

    I am making a twoway graph as follows:

    Code:
    twoway (scatter daysMED week if  dateWeek<2841 & dateWeek>2715, color(gs10)) ///
    (lowess daysMED week if  dateWeek<2841 & dateWeek>2715, bwidth(2) lcolor(black)) ///
    (scatter daysMED week if dateWeek>2841 & dateWeek<2848, color(gs10)  ) ///
    (lowess daysMED week if dateWeek>2841 & dateWeek<2848 , bwidth(2) lcolor(black)) ///
    (scatter daysMED week if dateWeek>2847 & dateWeek<2970, color(gs10)) ///
    (lowess daysMED week if dateWeek>2847 & dateWeek<2970, bwidth(2) lcolor(black)), xlabel(2767(20)2922, angle(45)) xline(2841) xline(2848) ///
    xtitle("")  legend(col(2) row(3)) legend(size(vsmall))
    It looks as follows:


    Click image for larger version

Name:	Statalist_1.png
Views:	1
Size:	105.6 KB
ID:	1541670
    I would like to change the legend to:
    2013w12-2014w33 Period before tramadol scheduling
    2014w34-2014w41 Period after tramadol scheduling and before hydrocodone upscheduling
    2014w42-2016w16 Period after hydrocodone upscheduling.

    How can I do that?

    Similarly, in another graph:
    Code:
    twoway (lfitci daysMED week if dateWeek>2833 & dateWeek<2842 ) ///
    (lfitci daysMED week if dateWeek>2840 & dateWeek<2849 ) ///
    (lfitci daysMED week if dateWeek>2847 & dateWeek<2855 ), xlabel(2834 "2014w27" 2836 "2014w29" 2838 "2014w31" 2840 "2014w33" ///
    2842 "2014w35" 2844 "2014w37" 2846 "2014w39" 2848 "2014w41" 2850 "2014w43" 2852 "2014w45" 2854 "2014w47", ///
    angle(45)) xline(2841) xline(2848) ///
    xtitle("")  legend(col(2) row(3)) legend(size(vsmall))
    which looks as follows:

    Click image for larger version

Name:	Statalist_2.png
Views:	1
Size:	73.5 KB
ID:	1541671

    I would again like to have the legend as :

    2014w27-2014w33 Period before tramadol scheduling
    2014w34-2014w41 Period after tramadol scheduling and before hydrocodone upscheduling
    2014w42-2014w47 Period after hydrocodone upscheduling.

    Same question, how can I customize this legend as well?

    I will be grateful for your help.
    Sincerely,
    Sumedha.

  • #2
    Same procedure for both:


    2014w42-2016w16 Period after hydrocodone upscheduling
    Code:
    legend(order(1 "2013w12-2014w33: before tramadol " 3 "2014w34-2014w41: after tramadol and before hydrocodone" 5 "after hydrocodone"))
    Roman

    Comment


    • #3
      Thank you Roman. It works, but can I supress the picture of the *dot* before the legend?

      Click image for larger version

Name:	Statalist_3.png
Views:	1
Size:	103.9 KB
ID:	1541678

      Thank you again for your help.
      Sincerely,
      Sumedha.

      Comment


      • #4
        For the legend to be worth adding at all, you need different marker symbols and/or colours for the three groups of data.

        Otherwise you are better off with a customised xtitle()

        I have to be curious. You are using Stata weeks, but Stata weeks each year always start on 1 January and end with a 8 or 9 day week ending 31 December, so there are always 52 weeks in each year. That doesn't match any other definition of week I've seen used for recording data.
        Last edited by Nick Cox; 17 Mar 2020, 11:52.

        Comment


        • #5
          Chiming with Nick, what is the point of legend if the legend marker is not used? You probably then need to use legend(off) option and add meaningful 'xtitle' (yet not a suitable choice). I think, it would be better if your choose different colors for markers using 'mcol()' option after each 'scatter' function instead 'gs10' color scheme for all and get different color of legends.
          Roman

          Comment


          • #6
            Thank you so much Nick and Roman. You are right. I dropped the legend (legend(off)) and instead included a caption. Does the job! Thank you again.

            Comment


            • #7
              Be useful for others if you could post the commands you gave and the final result. Thanks.

              Comment

              Working...
              X