Announcement

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

  • rearranging legend at the bottom as a line

    hello, i've been playing around with legend positions, and can't find the right one that doesn't obscure the graph. I've tried all the clock face positions.

    Is it possible to have the legend below the x axis i.e not in the graph space?

    I've got a chunky legend as you can see

    Many thanks
    Martinique

    Click image for larger version

Name:	Capture1.PNG
Views:	1
Size:	14.4 KB
ID:	1755334

  • #2
    Have you tried the pos location option?

    Code:
    sysuse auto, clear
    graph twoway scatter mpg weight, legend(on pos(6))

    Comment


    • #3
      Martinique / Tara Boyle

      Please show us your graph command and the whole of your graph if you want good advice.

      Comment


      • #4
        Hi there i used the following code

        I would really appreciate some input, as this code due to the large dataset takes half a day to run... so when I change the pos(x) number, it takes another half a day to see if it works.
        I've tried (3 o'clock, 10 o'clock, 6 o'clock) and at some point the legend obscures the lines. Is there a way I can sort the legend as a line at the bottom of the x axis. From what I read in previous posts, this may not be possible...

        Appreciate your input.

        Code:
        twoway lowess total_postop age if procedure==0, lp(dash) ///
        || lowess total_preop age if procedure==0, lp(dash) ///
        || lowess total_postop age if procedure==1 ///
        || lowess total_preop age if procedure==1, ///
        title("6 month OKS") xtitle("Age") ///
        graphregion(color(white)) xlabel(30[10]100) ///
        legend(order(1 "Post op procedure=0" 2 "Preop procedure=0" 3 "Postop procedure=1" 4 " Preop procedure=1") ///
        ring(0) cols(1) pos(10))

        Comment


        • #5
          If you say ring(0) there is always a risk of occluding the data unless you know that one corner (say) of the graph is never visited by the data.

          You can reduce risks by cutting "procedure" from all and standardising and trimming like say

          Code:
           order(1 "Postop No" 2 "Preop No" 3 "Postop Yes " 4 "Preop Yes")
          Additionally, or otherwise, consider ring(1) pos(6) row(1).
          Last edited by Nick Cox; 05 Jun 2024, 15:52.

          Comment


          • #6
            Hi I've been running this again... as it takes 5hours go run, need it to run at night so as I can work on something else in the morning

            This time I used:

            Code:
            twoway lowess total_postop age if procedure==0, lp(dash) ///
            || lowess total_preop age if procedure==0, lp(dash) ///
            || lowess total_postop age if procedure==1 ///
            || lowess total_preop age if procedure==1, ///
            title("6 month OKS") xtitle("Age") ///
            graphregion(color(white)) xlabel(30[10]100) ///
            legend(order(1 "Post op procedure=0" 2 "Preop procedure=0" 3 "Postop procedure=1" 4 " Preop procedure=1") ///
            ring(1) row(1) pos(6))
            However the legend text was still overlapping, I don't want to shorten the legend text as you had kindly advised in Post 5

            Tomorrow morning I'm going to run:
            ring(1) row(2) pos(6))

            This should present the legend in 2 rows and avoid any overlapping.
            Just wanted to check if you had any other advice, as this is going to take another 5hrs to run

            Q2 = Also do you know why the y-axis title is so close to the Y-axis?
            I'm just being pedantic here, I can live with it. However, just curious

            Click image for larger version

Name:	Screenshot 2024-07-17 at 22.48.45.png
Views:	1
Size:	185.8 KB
ID:	1759053

            Comment


            • #7
              I would suggest you use a small dataset to make sure you like the graph you get before running in the big dataset. For example, something like:

              Code:
              clear
              
              set obs 100
              set seed 12345
              gen procedure = runiform() > 0.3
              gen age = runiform(20, 90)
              gen total_postop = runiform(10, 100)
              gen total_preop = runiform(10, 100)
              
              twoway lowess total_postop age if procedure==0, lp(dash) ///
              || lowess total_preop age if procedure==0, lp(dash) ///
              || lowess total_postop age if procedure==1 ///
              || lowess total_preop age if procedure==1, ///
              title("6 month OKS") xtitle("Age at surgery")    ///
              ytitle("6 month postop OKS") ///
              graphregion(color(white)) xlabel(30[10]100) ///
              legend(order(1 "Post op procedure=0" 2 "Preop procedure=0" 3 "Postop procedure=1" 4 " Preop procedure=1") ///
              ring(1) row(2) pos(6))
              which produces the following on my machine.
              Click image for larger version

Name:	ex1.png
Views:	1
Size:	99.6 KB
ID:	1759056
              one possible edit is to change "Post op.." for first legend key to "Postop" to be consistent with the third key. Also note, the colors in your second graph are very different from your first graph, instead of 4 different colors for 4 overlaid plots, you have only two different colors blue and red. Also the text for the first key is "Post-Op 6 months OKS in TRR" which suggest you are running something else from the commands you show.
              Last edited by Hua Peng (StataCorp); 17 Jul 2024, 16:56.

              Comment

              Working...
              X