Announcement

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

  • Twoway lfitci, dissapear "graphs by"

    Dear Stata users,
    I am using the following code:
    twoway (lfitci openness age), by(immig) ytitle("Openness") xtitle("Age") name(openness2)
    twoway (lfitci conscientiousness age), by(immig, legend(off)) ytitle("Conscientiousness") xtitle("Age") legend(off) name(conscientiousness2)
    twoway (lfitci extraversion age), by(immig, legend(off)) ytitle("Extraversion") xtitle("Age") legend(off) name(extraversion2)
    twoway (lfitci agreeableness age), by(immig, legend(off)) ytitle("Agreeableness") xtitle("Age") legend(off) name(agreeableness2)
    twoway (lfitci neuroticism age), by(immig, legend(off)) ytitle("Neuroticism") xtitle("Age") legend(off) name(neuroticism2)
    grc1leg openness2 conscientiousness2 extraversion2 agreeableness2 neuroticism2, legendfrom(openness2) ycommon xcommon title("Personality over the life cycle")

    And I obtained the image attached. Is it possible to obtain these graphs without appearing "Graphs by immig" and the legend appearing only once? Also, I used "ycommon" but my y-axis for "Neuroticism" is different from the others. How can I change that?
    I am using Stata 14.
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	46.9 KB
ID:	1538051
    Last edited by Marta Oliveira; 23 Feb 2020, 13:58.

  • #2
    The "Graphs by immig" is either a note or a caption, I forget which, so try adding note(off) or caption(off) and see which one makes it go away.

    Regarding the legend on the first plot, have you tried turning it off as you did on the other four plots? Perhaps twoway will remember what the legend would have been had it been on.

    Finally, it is not just Neuroticism that has a different axis scale, if you look carefully. The output of help graph combine (which grc1leg is built on) tells us that ycommon and xcommon do not apply to graphs drawn with the by() option. You may have to manually specify the y axis on each of your twoway commands.

    Comment


    • #3
      Dear William Lisowski,
      Thank you for your suggestions.
      Regarding adding note(off), if I do that, instead of appearing "Graphs by immig", it appears "off". If I do caption(off) it appears off below "Graphs by immig". Relatively to turning off the legend in the four plots, if I do that, no legend appears.

      twoway (lfitci openness age), by(immig, legend(off) caption(off)) ytitle("Openness") xtitle("Age") name(openness2)
      twoway (lfitci conscientiousness age), by(immig, legend(off) note(off)) ytitle("Conscientiousness") xtitle("Age") legend(off) name(conscientiousness2)
      twoway (lfitci extraversion age), by(immig, legend(off) note(off)) ytitle("Extraversion") xtitle("Age") legend(off) name(extraversion2)
      twoway (lfitci agreeableness age), by(immig, legend(off) note(off)) ytitle("Agreeableness") xtitle("Age") legend(off) name(agreeableness2)
      twoway (lfitci neuroticism age), by(immig, legend(off) note(off)) ytitle("Neuroticism") xtitle("Age") legend(off) name(neuroticism2)
      grc1leg openness2 conscientiousness2 extraversion2 agreeableness2 neuroticism2, ycommon xcommon title("Personality over the life cycle")

      Comment


      • #4
        I guess the trick for suppressing the note is note("") .

        Comment


        • #5
          And I found that if I left the legend on in all my graphs, grc1leg turned them all off. So remove all the legend(off) in your code.
          Code:
          sysuse auto
          scatter price weight mpg if foreign==0, name(g0)
          scatter price weight mpg if foreign==1, name(g1)
          grc1leg g0 g1

          Comment


          • #6
            William Lisowski, your suggestion about note("") worked perfect, thank you. But I removed all legend(off) and it appears in the end 6 legends:
            twoway (lfitci openness age), by(immig, note("")) ytitle("Openness") xtitle("Age") name(openness2)
            twoway (lfitci conscientiousness age), by(immig, note("")) ytitle("Conscientiousness") xtitle("Age") name(conscientiousness2)
            twoway (lfitci extraversion age), by(immig, note("")) ytitle("Extraversion") xtitle("Age") name(extraversion2)
            twoway (lfitci agreeableness age), by(immig, note("")) ytitle("Agreeableness") xtitle("Age") name(agreeableness2)
            twoway (lfitci neuroticism age), by(immig, note("")) ytitle("Neuroticism") xtitle("Age") name(neuroticism2)
            grc1leg openness2 conscientiousness2 extraversion2 agreeableness2 neuroticism2, ycommon xcommon title("Personality over the life cycle")

            Comment


            • #7
              I think that you have uncovered a flaw in grc1leg relating to graphs created using the -by- option. I will tag Vince Wiggins (StataCorp), the author of grc1leg who may look to address it in a future update. Here is a reproducible example and my workaround.

              This does not get rid of the legends from the individual by() graphs.

              Code:
              sysuse auto, clear
              tw lfitci weight mpg , by(foreign) name(g0, replace)
              tw lfitci weight mpg , by(rep78) name(g1, replace)
              grc1leg g0 g1, ycommon xcommon title("My combined graph")
              Also, suppressing the legends using by(byvar, leg(off)) will not solve it as grc1leg will not have a legend to retrieve from the individual graphs. A third possibility will be to allow grc1leg to pick a legend from a saved graph that is not part of the graphs to be combined, but this option is not possible as the -legendfrom()- option requires that the legend is retrieved from one of the graphs specified in the command. Therefore, a workaround is to suppress the legend in all the by graphs but one, and recreate the remaining twoway by graph using the if qualifier and grc1leg.

              Code:
              sysuse auto, clear
              *SUPPRESSING THE LEGEND
              tw lfitci weight mpg , by(rep78, leg(off) note("")) name(g1, replace)
              *TW WAY GRAPH USING IF & GRC1LEGEND
              tw (lfitci weight mpg if foreign==0), title("Domestic cars") name(g3, replace)
              tw (lfitci weight mpg if foreign==1), title("Foreign cars") name(g4, replace)
              grc1leg g3 g4, legendfrom(g3) name(g5, replace)
              *NOW USE GRC1LEG TO COMBINE ALL GRAPHS
              grc1leg g1 g5,  ycommon xcommon title("My combined graph") legendfrom(g5)
              Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.3 KB
ID:	1538133

              Comment


              • #8
                I will note that grc1leg2 is derived from grc1leg and has been under recent development. It is community contributed software but available from the developer rather than from SSC: search grc1leg2 returns a link to the following command

                Code:
                net describe grc1leg2, from(http://digital.cgdev.org/doc/stata/MO/Misc)
                I installed it and unfortunately it did not solve the problem, but mention it here so others don't go down this path.

                I wonder if the flaw is not in grc1leg and grc1leg2 but rather a side effect of the by() option in the underlying graphs - in the same way that by() defeats ycommon and xcommon in graph combine.

                If I run
                Code:
                sysuse auto, clear
                scatter weight length mpg , name(g0, replace)
                scatter weight length mpg , name(g1, replace)
                graph combine g0 g1, name(gg)
                and then open any of the three graphs in the Graph Editor window, I can select its legend and in the Legend properties window, on the Advanced tab, check the Hide Legend box to remove the legend from the graph. This is what I was going to suggest as a workaround, and we note that in post #5 grc1leg worked as expected.

                If instead I run
                Code:
                sysuse auto, clear
                scatter weight length mpg , by(foreign) name(g0, replace)
                scatter weight length mpg , by(foreign) name(g1, replace)
                graph combine g0 g1, name(gg)
                and then open any of the three graphs in the Graph Editor window, I can select its legend and in the Legend properties window, on the Advanced tab, I find that the Hide Legend box is already checked(!!!). No amount of fiddling will make it go away.

                Comment


                • #9
                  Thank you William Lisowski and Andrew Musau for all your comments. I don't like the idea of the "My combined graph" because the graphs in the end do not have all the same size (I also tested for my case). What I will end up doing: use legend(off) for all graphs, and copy&paste the legend using Paint.

                  Comment


                  • #10
                    I have found a hack for solving the problem, but perhaps easier than using Paint after exporting the combined graph if you're going to do this more than once.

                    This time I ran
                    Code:
                    sysuse auto, clear
                    scatter weight length mpg , by(foreign, legend(off)) name(g0, replace)
                    scatter weight length mpg , by(foreign, legend(off)) name(g1, replace)
                    scatter weight length mpg , by(foreign) name(dummy, replace)
                    grc1leg g0 g1 dummy, name(gg) legendfrom(dummy)
                    and then opened the combined graph (that is, gg) in the Graph Editor window. I clicked the disclosure arrow next to plotregion1, I clicked on graph3 to select it, and in the Graph Properties window, on the Advanced tab, clicked the Hide graph box to remove this dummy graph with its legend. By default the Collapse when hidden box was checked, so the hidden graph takes up no area and the other graphs expand.

                    Then I did all that again and used the Graph Editor Recorder to record what I did and save the recording.

                    Then I ran the code again, but this time in the Graph Editor I replayed the saved recording and it again made the dummy graph vanish.

                    Then I found the recording in the adopath personal ado directory, opened it in a text editor, and (using a hint I learned ages ago on Statalist) confirmed that adding
                    Code:
                    gr_edit .plotregion1.graph3.draw_view.setstyle, style(no)
                    to the code above, the disappearance happens without any effort on my part.

                    Comment


                    • #11
                      Quite a hack William Lisowski, but I will say that since Marta has the same x variable for all graphs, it should be possible to plot all the graphs using the by() option without the need for graph combine by reorganizing the data. In many ways, gr combine is an alternative to using the by() option in cases where the latter is not feasible, which could explain its difficulties with combining by() graphs.

                      Comment

                      Working...
                      X