Announcement

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

  • Graph Twoway with Two Y-Axes: Fine Tuning Formatting

    I am using Stata 15.1. My data come from a clinical trial of two diets (coded 1 and 2). Various measures were recorded at each of four visits, such as weight, lipids, and insulin. I would like to graph how two of the measures varied by diet across each of the four visits. I'm using a two-way line graph to do this.

    In this example, the two measures to be included in the same graph are cholesterol ("chol") and insulin resistance ("lnIR"). Note that because lnIR is a log transformation, this measure has negative values.

    MY QUESTION: I cannot seem to make adjustments to the formatting of second of the two y-axes. When I specify an axis title, the title goes to the wrong axis. When I specify a height suboption for the title, the height suboption is applied to the first axis only. I have tried inserting the additional suboption yaxis(#) within the "ytitle" option, but that hasn't worked so far. I've been scouring manuals and older postings but have not found an answer yet.

    Code:
    separate chol, by(diet)
    separate lnIR, by(diet)
    collapse (mean) chol1 chol2 lnIR1 lnIR2, by(diet visit)
    graph twoway ///
        (line chol1 visit, yaxis(1) ytitle("Mean Cholesterol", height(5)) ylabel( , labsize(small))) ///
        (line lnIR1 visit, yaxis(2) ytitle("Mean Ln IR", height(5)) ylabel( , labsize(small))), ///
        legend(off) graphregion(color(white)) note("")
    graph export twoway_example.png
    Click image for larger version

Name:	twoway_example.png
Views:	1
Size:	119.2 KB
ID:	1545888

    Could you please assist? Also, any other suggestions about how to approach this graph are welcome! Ideally, for example, I would use an estimation function that would allow me to display confidence intervals. Also, I'd like to display the other two lines, associated with the second of the two diets, alongside the first. I suppose if I use combine graph I could show them side-by-side.

    Thank you in advance for your assistance.

    Lois

  • #2
    In the ytitle() statements you need to specify the axis with the axis(#) option:

    Code:
    sysuse auto,clear
    scatter price mpg, ytitle("Axis 1", axis(1)) yaxis(1) ///
     || scatter weight mpg, ytitle("Axis 2", axis(2)) yaxis(2)
    Also, the height() option in ytitle() is only changing the dimension of the text box not the text itself. Perhaps you want size()

    Comment


    • #3
      Suggestion of other approach: https://www.statalist.org/forums/for...ailable-on-ssc

      Comment


      • #4
        SCOTT & NICK: thank you very much for your help. Some specific remarks on both of your suggestions below.

        SCOTT, thank you for pointing out the need to specify the axis when modifying a graph with 2 y-axes. I had tried that earlier, and failed. After re-examining my effort, I found the answer in two places:

        1) The positioning of the axis specification ("axis(1)" or "axis(2)") had to be right. The positioning depended on whether it came at the level of the graph twoway suboption or as an option couched within the suboption (after a comma).

        Here is an example where the axis specification was made at the level of the suboption yscale of graph twoway:
        Code:
        yscale(axis(2))
        Here is an example of the axis specification couched within the ylabel suboption of graph twoway:
        Code:
        ylabel(185(5)200, yaxis(1))
        2) The gap between the axis title and the axis labels is titlegap, a component of the suboption yscale. I specified that I wanted the titlegap to be 5x the default.
        Code:
        yscale(axis(2) titlegap(*5))
        Here is the complete revised code and new graph:
        Code:
        separate chol, by(diet)
        separate lnIR, by(diet)
        collapse (mean) chol1 chol2 lnIR1 lnIR2, by(diet visit)
        graph twoway ///
            (line chol1 visit, yaxis(1) ytitle("Mean Cholesterol", axis(1) size(small)) ylabel(185(5)200, axis(1)) yscale(axis(1) titlegap(*5)) ylabel( , axis(1) labsize(small))) ///
            (line lnIR1 visit, yaxis(2) ytitle("Mean Ln IR", axis(2) size(small)) ylabel(.90(.1)1.2, axis(2)) yscale(axis(2) titlegap(*5)) ylabel( , axis(2) labsize(small))), ///
            legend(off) graphregion(color(white)) note("") title("Low Carb Diet")
        Click image for larger version

Name:	twoway_example_v2.png
Views:	1
Size:	104.2 KB
ID:	1546074

        NICK, thank you for the suggestion to try multilines. Thank you Kit Baum for writing it, and thank you Nick for adding a modification. I employed multilines to graph a number of findings related to each diet. Then I combined the two multiline files using graph combine. The code and result below. I'm sure there is a way to get some better formatting on the multilines graph, but I was not too successful trying to modify it using the line options.

        Code:
        preserve
        separate weight, by(diet)
        separate chol, by(diet)
        separate trig, by(diet)
        separate ldl, by(diet)
        separate hdl, by(diet)
        separate lnIR, by(diet)
        collapse (mean) weight1 weight2 chol1 chol2 trig1 trig2 ldl1 ldl2 hdl1 hdl2 lnIR1 lnIR2, by(diet visit)
        multiline weight1 chol1 trig1 ldl1 hdl1 lnIR1 visit if diet ==1, recast(connected) ///
            mylabels(`" `" "LC Weight" "Mean (kg)" "' `" "LC Cholesterol" "mean (mg/dL)" "' `"  "LC Triglycerides" "mean (mg/dL)" "' `" "LC LDL" "Mean (mg/dL)" "' `" "LC HDL" "Mean (mg/dL)" "' `" "LC ln IR (HOMA)" "mean" "' "') ///
            ytitle( , axis(1) size(small)) ylabel( , labsize(small)) xtitle( , size(med)) name(LC_multiple_ex)
        graph export multiples_LCexample.png, replace
        restore
        
        preserve
        separate weight, by(diet)
        separate chol, by(diet)
        separate trig, by(diet)
        separate ldl, by(diet)
        separate hdl, by(diet)
        separate lnIR, by(diet)
        collapse (mean) weight1 weight2 chol1 chol2 trig1 trig2 ldl1 ldl2 hdl1 hdl2 lnIR1 lnIR2, by(diet visit)
        multiline weight2 chol2 trig2 ldl2 hdl2 lnIR2 visit if diet ==2, recast(connected) ///
            mylabels(`" `" "LF Weight" "Mean (kg)" "' `" "LF Cholesterol" "mean (mg/dL)" "' `"  "LF Triglycerides" "mean (mg/dL)" "' `" "LF LDL" "Mean (mg/dL)" "' `" "LF HDL" "Mean (mg/dL)" "' `" "LF ln IR (HOMA)" "mean" "' "') ///
            ytitle( , axis(1) size(small)) ylabel( , labsize(small)) xtitle( , size(med)) name(LF_multiple_ex)
        graph export multiples_LFexample.png, replace
        restore
        
        graph combine LC_multiple_ex LF_multiple_ex
        graph export multiples_combined.png
        Click image for larger version

Name:	multiples_combined.png
Views:	1
Size:	195.8 KB
ID:	1546075

        I am always amazed at the time you give us on the Stata Forum and the kind support you and others offer. Very much appreciated!

        Comment


        • #5
          Thanks for the appreciation. FWIW, I am the author of multiline and Kit Baum is the maintainer of SSC (for more than 20 years now).

          Comment

          Working...
          X