Announcement

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

  • How to make different graphs in a combined graph share one legend box?

    Hello,

    I combine several graphs (produced by -xtline-) together. However, the default option is that each individual graph has its own legend box. So how can I make all the graphs share just one legend box?
    Each graph has exactly the same legends.

    thank you very much!

  • #2
    I have not tested it yet, but there is a user-written program - grc1leg - aimed, as the title implies, at using graph combine with one legend.

    Hopefully that helps!
    Best regards,

    Marcos

    Comment


    • #3
      There is also grc1leg2, which is an 'update' of grc1leg.

      findit grc1leg2.
      grc1leg2, like graph combine, arrays separately drawn graphs as panels in a single combined graph. In addition, grc1leg2 suppresses the legends in the individual graphs and adds a common legend taken from one of them. Optionally grc1leg2 can also replace the separate xtitles on each of the individual graphs with a single b1title on the combined graph. Optionally grc1leg2 can also alter the textsizestyle of the text labels in the common legend. All other attributes of the common legend are inherited from the legend on the component graph.


      Cheers,
      Martyn



      Comment


      • #4
        Originally posted by Marcos Almeida View Post
        I have not tested it yet, but there is a user-written program - grc1leg - aimed, as the title implies, at using graph combine with one legend.

        Hopefully that helps!
        Dear Marcos,

        Thank you so much. This indeed works!

        Comment


        • #5
          Originally posted by Martyn Sherriff View Post
          There is also grc1leg2, which is an 'update' of grc1leg.

          findit grc1leg2.
          grc1leg2, like graph combine, arrays separately drawn graphs as panels in a single combined graph. In addition, grc1leg2 suppresses the legends in the individual graphs and adds a common legend taken from one of them. Optionally grc1leg2 can also replace the separate xtitles on each of the individual graphs with a single b1title on the combined graph. Optionally grc1leg2 can also alter the textsizestyle of the text labels in the common legend. All other attributes of the common legend are inherited from the legend on the component graph.


          Cheers,
          Martyn


          Hi Martyn, thank you!

          Comment


          • #6
            Hi,
            I am using grc1leg2 to combine 4 graphs. Three of the graphs have the same legend but one of them has a different legend. Is it possible to create one legend which includes the information from the two different legends? Essentially, I do not want to have a legend for each graph but one legend that includes the information from the two different legends.

            Thanks for your help.

            Best,
            Mara

            Comment


            • #7
              Hi Mara, you should have started a new thread as you are not adding anything to this question posted by someone else rather you are asking a new question. However, on your point, I think one option is to use 'grc1leg' for those which have common legend, save it and then combine it with the other graph with separate legend using Stata's 'graph combine' command.

              Roman

              Comment


              • #8
                Thanks Roman.

                In your suggested solution the resulting graph would include a big legend for the three graphs with the same legen and a small legend at the fourth graph, right? I wonder whether it is possible to merge the two legends to one big legend for the whole graph?

                Comment


                • #9
                  There may be simpler solutions, but without a sight of the graphs, the data or the original commands you used, that can only be a speculation.

                  See #4 in https://www.statalist.org/forums/for...axis-variables for combined graphs of panel data with no legend whatsoever.

                  Your problem may be quite different, but again the point is that we can't tell.

                  Comment


                  • #10
                    Regarding Maraa Barschkett's question: I have found the following workaround to achieve your goal. The ado program grc1leg2 features the ability to combine graphs, display the legend from one graph and hide the same graph. In this example, I am using just two graphs and combine them into a single graph and display the legend including all values (Example 1). To achieve this, I just plot a third graph with all groups, then use grc1leg2 to combine all three graphs but surpress the graph from which I am taking the legend. However you will have to manually adjust the colors for the lines in the graph(s) using plot#opt (Example 2). Important parts are in bold.

                    Example 1: Common legend but wrong colors
                    Code:
                    * Load dataset and set to panel data
                    use https://www.stata-press.com/data/r18/patents.dta
                    xtset classid year
                    
                    * Plot graphs
                    xtline uspatents if inlist(classid, 65, 455), overlay saving(g1.gph, replace)
                    xtline uspatents if inlist(classid, 345215, 345280), overlay saving(g2.gph, replace)
                    
                    * This plot contains all panel groups, but it looks messy, I just use it take the graph and hide the rest
                    xtline uspatents if inlist(classid, 345215, 245280, 455, 65), overlay saving(g3.gph)
                    grc1leg2 g1.gph g2.gph g3.gph, legendfrom(g3.gph) hide
                    Example 2: Common legend with manually adjusted colors
                    Code:
                    * Load dataset and set to panel data
                    use https://www.stata-press.com/data/r18/patents.dta
                    xtset classid year
                    
                    * Plot graphs with manually adjusting the colors
                    xtline uspatents if inlist(classid, 65, 455), overlay saving(g1.gph, replace)
                    xtline uspatents if inlist(classid, 345215, 345280), overlay saving(g2.gph, replace) plot1opt(lcolor(stc3)) plot2opt(lcolor(stc4))
                    xtline uspatents if inlist(classid, 65, 455, 345215, 345280), overlay saving(g3.gph, replace) legend(col(4))
                    grc1leg2 g1.gph g2.gph g3.gph, row(2) legendfrom(g3.gph) hide
                    __________________________________________________ __________

                    Cheers, Felix
                    Stata Version: MP 18.0
                    OS: Windows 11

                    Comment


                    • #11
                      graph combine is indeed often useful and even essential for some purposes. But sometimes when people resort to work-arounds such as grc1leg2 it seems to be a case of proceeding further in an ill-advised direction.

                      One alternative was written up in https://journals.sagepub.com/doi/pdf...36867X20976341 In essence, using a by() option can be an alternative to graph combine.

                      Moreover, a legend is at best a necessary evil -- the necessity being a matter of needing to explain what you're showing and the evil being committing the reader (the designer too) to back-and-forth between legend and plot. A simple slogan is: Lose the legend! Kill the key! (if you can).

                      Although the example in #10 is reproducible (thanks!), otherwise it seems a little contrived to me. Small counts as time series aren't especially easy to think about.

                      The problem makes perfect sense as wanting to show two panels in one graph and a different two panels in another graph. There are other solutions to that. I will just use a different example.

                      Code:
                      webuse grunfeld, clear 
                      
                      preserve 
                      
                      gen group = cond(inlist(company, 1,2), 1, cond(inlist(company, 9, 10), 2, .))
                      
                      separate invest, by(company) veryshortlabel 
                      
                      line invest1 invest2 invest9 invest10 year if group < ., by(group, legend(off) note("") yrescale) || scatter invest1 invest2 invest9 invest10 year if year == 1954, ms(none ..) mla(company company company company) mlabsize(medium ..) xtitle("") subtitle("", fcolor(none)) mlabcolor(stc1 stc2 stc3 stc4) ytitle("Investment (million USD, 1947 prices)") ysc(titlegap(*0.2))
                      Click image for larger version

Name:	notcombine.png
Views:	1
Size:	76.0 KB
ID:	1739847


                      General and specific points arise here on different levels.

                      * To use a by() option, a suitable grouping variable may need to be created.

                      * Different time series in different colours can be shown easily if they are copied to different variables. separate is intended largely for this kind of purpose.

                      * The legend can be cut whenever direct labelling is possible. With time series, the natural position for direct labels is usually to the right of the last value, but other choices can make as much or more sense. Here it's fortuitous but also fortunate that the axis labels by default end in 1955, the last value is for 1954 and panel identifiers are all short, so there is no space problem. At worst you may need to extend the x axis with xsc().

                      * With a little work you can arrange that the marker labels have the same colours as the time lines. This is unlikely to offend yet adds some psychological linkage.

                      * The grouping here into groups 1 and 2 is not otherwise of interest, but often you could and should arrange value labels for each group to be shown as subtitles.

                      * Note how the x axis really doesn't need a title here.

                      * Preferring logarithmic scale makes good sense too for these data.


                      Comment


                      • #12
                        The thread linked in #4

                        https://www.statalist.org/forums/for...axis-variables

                        remains highly pertinent.

                        Comment

                        Working...
                        X