Announcement

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

  • Expand yaxis in marginsplot

    I'm doing a marginsplot after a multilevel regression analysis with an addplot of two medians. Marginsplot and 1 of the medians belongs to the left yaxis, the other median belongs to the right yaxis.
    I would like to expand the left yaxis belonging to my marginsplot. Currently the left yaxis is ranging from 77-85, however I would like it to be going from 0 to 100.
    I have tried "yscale" and "ylabel" without any luck.

    My commands are:

    * Multilevel regression:
    mixed n_int_cr i.years_since_first_li i.sex c.age || dw_ek_borger:

    * Graph
    quietly margins, at(years_since_first_li= (1 (1) 10))
    marginsplot, recast(line) recastci(rarea) plot1opts(lcolor(gs8)) ciopt(color(black%20)) ytitle("µmol/L") title("Creatinine") xtitle("Lithium treatment (years)") addplot(line median_cr_intsv_grp years_since_first_li, yaxis(1) ||line median_li_intsv_grp years_since_first_li, yaxis(2) ylabel(0 (0.2) 1, axis(2)) yscale(alt axis(2)) ytitle(mmol/L, axis(2)))



    So my questions is: How do i change the yaxis?


    Thank you,
    Anne Joergensen

  • #2
    Usually, adding

    Code:
    ysc(r(0 100))
    should do it, but there is little to go by here without a reproducible example. See FAQ Advice #12 for details on how to improve your query if the above is not helpful.

    Comment


    • #3
      Thank you Mr. Masau. I just tried adding the command "ycr(r( 0 100))", however it does not change the yaxis.


      Here is an example of my data:

      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(id years_since_first_li egfr median_cr median_li)
      1 1 . 70 .6
      1 1 94.01551 70 .6
      1 1 . 70 .6
      1 1 . 70 .6
      1 1 100.28333 70 .6
      1 1 107.0182 70 .6
      1 1 . 70 .6
      1 1 . 70 .6
      1 1 98.77029 70 .6
      1 1 98.43136 70 .6
      1 1 . 70 .6
      1 1 . 70 .6
      1 1 95.5513 70 .6
      1 1 . 70 .6
      1 1 114.12547 70 .6
      1 1 . 70 .6
      1 1 . 70 .6
      2 1 . 70 .6
      2 1 108.67442 70 .6
      2 4 110.69234 74 .62
      2 1 91.84961 70 .6
      2 4 . 74 .62
      2 2 . 72 .61
      2 1 119.24886 70 .6
      2 1 107.88464 70 .6
      2 3 . 69 .63
      2 1 . 70 .6
      2 1 . 70 .6
      2 1 106.02055 70 .6
      2 4 . 74 .62
      2 1 102.38049 70 .6
      2 3 . 69 .63
      2 2 107.44714 72 .61
      2 1 91.15569 70 .6
      2 1 111.14088 70 .6
      2 3 . 69 .63
      2 1 107.90369 70 .6
      2 2 . 72 .61
      2 1 103.01714 70 .6
      2 2 . 72 .61
      2 2 . 72 .61
      2 3 . 69 .63
      2 1 89.61317 70 .6
      2 4 . 74 .62
      2 3 111.31185 69 .63
      2 1 91.93996 70 .6
      2 3 128.6277 69 .63
      2 4 . 74 .62
      2 2 . 72 .61
      2 1 . 70 .6
      2 1 99.09264 70 .6
      2 1 122.09425 70 .6
      2 2 98.56636 72 .61
      2 4 . 74 .62
      2 2 . 72 .61
      2 4 . 74 .62
      3 4 78.320114 74 .62
      3 4 91.62031 74 .62
      3 4 80.99072 74 .62
      3 1 70.18676 70 .6
      3 1 . 70 .6
      3 2 81.05975 72 .61
      3 2 99.34185 72 .61
      3 1 . 70 .6
      3 2 75.35548 72 .61
      3 5 66.53368 75 .6
      3 3 97.57891 69 .63
      3 3 83.66706 69 .63
      3 3 . 69 .63
      3 2 94.40717 72 .61
      3 4 73.61089 74 .62
      3 5 . 75 .6
      3 3 . 69 .63
      3 5 78.56989 75 .6
      3 1 89.58671 70 .6
      3 2 87.87996 72 .61
      3 4 87.85493 74 .62
      3 4 77.927414 74 .62
      3 4 68.37572 74 .62
      3 3 74.81699 69 .63
      3 4 84.06474 74 .62
      3 5 77.9016 75 .6
      3 4 88.0406 74 .62
      3 1 . 70 .6
      3 4 74.81699 74 .62
      3 4 68.29689 74 .62
      3 4 78.53368 74 .62
      3 4 88.09995 74 .62
      3 1 82.5287 70 .6
      3 4 . 74 .62
      3 3 74.19598 69 .63
      3 4 . 74 .62
      3 4 76.82907 74 .62
      3 3 82.2957 69 .63
      3 4 66.27863 74 .62
      3 4 86.27592 74 .62
      3 2 96.755 72 .61
      3 2 96.47743 72 .61
      3 1 97.64122 70 .6
      3 2 84.20365 72 .61
      end

      * multilevel regression
      mixed egfr i.years_since_first_li || id:
      * Graf
      quietly margins, at(years_since_first_li= (1 (1) 10))
      marginsplot, recast(line) recastci(rarea) plot1opts(lcolor(gs8)) ciopt(color(black%20)) ytitle("µmol/L") title("egfr") xtitle("Lithium treatment (years)") ysc(r(0 100)) addplot(line median_cr years_since_first_li, yaxis(1) sort ||line median_li years_since_first_li, yaxis(2) ylabel(0 (0.2) 1, axis(2)) yscale(alt axis(2)) ytitle(mmol/L, axis(2)) sort)



      / Anne Joergensen

      Comment


      • #4
        Thanks for the data example. Push it into the -addplot()-

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(id years_since_first_li egfr median_cr median_li)
        1 1 . 70 .6
        1 1 94.01551 70 .6
        1 1 . 70 .6
        1 1 . 70 .6
        1 1 100.28333 70 .6
        1 1 107.0182 70 .6
        1 1 . 70 .6
        1 1 . 70 .6
        1 1 98.77029 70 .6
        1 1 98.43136 70 .6
        1 1 . 70 .6
        1 1 . 70 .6
        1 1 95.5513 70 .6
        1 1 . 70 .6
        1 1 114.12547 70 .6
        1 1 . 70 .6
        1 1 . 70 .6
        2 1 . 70 .6
        2 1 108.67442 70 .6
        2 4 110.69234 74 .62
        2 1 91.84961 70 .6
        2 4 . 74 .62
        2 2 . 72 .61
        2 1 119.24886 70 .6
        2 1 107.88464 70 .6
        2 3 . 69 .63
        2 1 . 70 .6
        2 1 . 70 .6
        2 1 106.02055 70 .6
        2 4 . 74 .62
        2 1 102.38049 70 .6
        2 3 . 69 .63
        2 2 107.44714 72 .61
        2 1 91.15569 70 .6
        2 1 111.14088 70 .6
        2 3 . 69 .63
        2 1 107.90369 70 .6
        2 2 . 72 .61
        2 1 103.01714 70 .6
        2 2 . 72 .61
        2 2 . 72 .61
        2 3 . 69 .63
        2 1 89.61317 70 .6
        2 4 . 74 .62
        2 3 111.31185 69 .63
        2 1 91.93996 70 .6
        2 3 128.6277 69 .63
        2 4 . 74 .62
        2 2 . 72 .61
        2 1 . 70 .6
        2 1 99.09264 70 .6
        2 1 122.09425 70 .6
        2 2 98.56636 72 .61
        2 4 . 74 .62
        2 2 . 72 .61
        2 4 . 74 .62
        3 4 78.320114 74 .62
        3 4 91.62031 74 .62
        3 4 80.99072 74 .62
        3 1 70.18676 70 .6
        3 1 . 70 .6
        3 2 81.05975 72 .61
        3 2 99.34185 72 .61
        3 1 . 70 .6
        3 2 75.35548 72 .61
        3 5 66.53368 75 .6
        3 3 97.57891 69 .63
        3 3 83.66706 69 .63
        3 3 . 69 .63
        3 2 94.40717 72 .61
        3 4 73.61089 74 .62
        3 5 . 75 .6
        3 3 . 69 .63
        3 5 78.56989 75 .6
        3 1 89.58671 70 .6
        3 2 87.87996 72 .61
        3 4 87.85493 74 .62
        3 4 77.927414 74 .62
        3 4 68.37572 74 .62
        3 3 74.81699 69 .63
        3 4 84.06474 74 .62
        3 5 77.9016 75 .6
        3 4 88.0406 74 .62
        3 1 . 70 .6
        3 4 74.81699 74 .62
        3 4 68.29689 74 .62
        3 4 78.53368 74 .62
        3 4 88.09995 74 .62
        3 1 82.5287 70 .6
        3 4 . 74 .62
        3 3 74.19598 69 .63
        3 4 . 74 .62
        3 4 76.82907 74 .62
        3 3 82.2957 69 .63
        3 4 66.27863 74 .62
        3 4 86.27592 74 .62
        3 2 96.755 72 .61
        3 2 96.47743 72 .61
        3 1 97.64122 70 .6
        3 2 84.20365 72 .61
        end
        
        * Multilevel regression:
        mixed egfr i.years_since_first_li || id:
        quietly margins, at(years_since_first_li= (1 (1) 5))
        set scheme s1color
        marginsplot, recast(line) recastci(rarea) plot1opts(lcolor(gs8)) ///
        ciopt(color(black%20)) ytitle("µmol/L") title("egfr") xtitle("Lithium treatment (years)") ///
        addplot(line median_cr years_since_first_li, yaxis(1) sort ysc(r(0 100)) ylab(0(10) 100) || ///
        line median_li years_since_first_li, yaxis(2) ylabel(0 (0.2) 1, axis(2)) ///
        yscale(alt axis(2)) ytitle(mmol/L, axis(2)) sort) leg(off)
        Res.:

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	64.8 KB
ID:	1682108

        Comment


        • #5
          It did it! Thank you very much.

          Comment


          • #6
            I have a follow-up question, Mr. Musau, concerning my x-axis.

            As you see above the variable years_since_first_li is coded 1, 2, 3....... However, what it actually means is that all observations taken within the first year is = 1, meaning the egfr could have been recorded on a random day within this year. (i.g. a test at year 1 = was taken day 0 until day 365. Year 2 = day 366 until day 731....). I would like to include that in the graph. Meaning the xaxis should start at 0 and the graphs (slope from the marginsplot and medians) should appear between 0-1, 1-2, 2-3, because the observations are recorded BETWEEN those timepoints. How would you suggest I do that?
            I hope I have described my problem adequate.

            Comment


            • #7
              Since you have a categorical time variable, you can only change the labels, either adding them directly or by relabeling the axis. I show the latter below. If you have the exact times that the recordings were taken, only then can you treat time as continuous.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(id years_since_first_li egfr median_cr median_li)
              1 1 . 70 .6
              1 1 94.01551 70 .6
              1 1 . 70 .6
              1 1 . 70 .6
              1 1 100.28333 70 .6
              1 1 107.0182 70 .6
              1 1 . 70 .6
              1 1 . 70 .6
              1 1 98.77029 70 .6
              1 1 98.43136 70 .6
              1 1 . 70 .6
              1 1 . 70 .6
              1 1 95.5513 70 .6
              1 1 . 70 .6
              1 1 114.12547 70 .6
              1 1 . 70 .6
              1 1 . 70 .6
              2 1 . 70 .6
              2 1 108.67442 70 .6
              2 4 110.69234 74 .62
              2 1 91.84961 70 .6
              2 4 . 74 .62
              2 2 . 72 .61
              2 1 119.24886 70 .6
              2 1 107.88464 70 .6
              2 3 . 69 .63
              2 1 . 70 .6
              2 1 . 70 .6
              2 1 106.02055 70 .6
              2 4 . 74 .62
              2 1 102.38049 70 .6
              2 3 . 69 .63
              2 2 107.44714 72 .61
              2 1 91.15569 70 .6
              2 1 111.14088 70 .6
              2 3 . 69 .63
              2 1 107.90369 70 .6
              2 2 . 72 .61
              2 1 103.01714 70 .6
              2 2 . 72 .61
              2 2 . 72 .61
              2 3 . 69 .63
              2 1 89.61317 70 .6
              2 4 . 74 .62
              2 3 111.31185 69 .63
              2 1 91.93996 70 .6
              2 3 128.6277 69 .63
              2 4 . 74 .62
              2 2 . 72 .61
              2 1 . 70 .6
              2 1 99.09264 70 .6
              2 1 122.09425 70 .6
              2 2 98.56636 72 .61
              2 4 . 74 .62
              2 2 . 72 .61
              2 4 . 74 .62
              3 4 78.320114 74 .62
              3 4 91.62031 74 .62
              3 4 80.99072 74 .62
              3 1 70.18676 70 .6
              3 1 . 70 .6
              3 2 81.05975 72 .61
              3 2 99.34185 72 .61
              3 1 . 70 .6
              3 2 75.35548 72 .61
              3 5 66.53368 75 .6
              3 3 97.57891 69 .63
              3 3 83.66706 69 .63
              3 3 . 69 .63
              3 2 94.40717 72 .61
              3 4 73.61089 74 .62
              3 5 . 75 .6
              3 3 . 69 .63
              3 5 78.56989 75 .6
              3 1 89.58671 70 .6
              3 2 87.87996 72 .61
              3 4 87.85493 74 .62
              3 4 77.927414 74 .62
              3 4 68.37572 74 .62
              3 3 74.81699 69 .63
              3 4 84.06474 74 .62
              3 5 77.9016 75 .6
              3 4 88.0406 74 .62
              3 1 . 70 .6
              3 4 74.81699 74 .62
              3 4 68.29689 74 .62
              3 4 78.53368 74 .62
              3 4 88.09995 74 .62
              3 1 82.5287 70 .6
              3 4 . 74 .62
              3 3 74.19598 69 .63
              3 4 . 74 .62
              3 4 76.82907 74 .62
              3 3 82.2957 69 .63
              3 4 66.27863 74 .62
              3 4 86.27592 74 .62
              3 2 96.755 72 .61
              3 2 96.47743 72 .61
              3 1 97.64122 70 .6
              3 2 84.20365 72 .61
              end
              
              * Multilevel regression:
              mixed egfr i.years_since_first_li || id:
              margins, at(years_since_first_li= (1/5)) 
              set scheme s1color
              marginsplot, recast(line) recastci(rarea) plot1opts(lcolor(gs8)) ///
              ciopt(color(black%20)) ytitle("µmol/L") title("egfr") xtitle("Lithium treatment (years)") ///
              addplot(line median_cr years_since_first_li, yaxis(1) sort ylab(0(10) 110) || ///
              line median_li years_since_first_li, yaxis(2) ylabel(0 (0.2) 1, axis(2)) ///
              yscale(alt axis(2)) ytitle(mmol/L, axis(2)) sort) leg(off) ///
              xlab(1 "0−1" 2 "1−2" 3 "2−3" 4 "3−4" 5 "4−5") 
              Res.:

              Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.5 KB
ID:	1682338



              Comment


              • #8
                Thank you!

                Comment

                Working...
                X