Announcement

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

  • Single legend for a combined graph

    Hello,

    I am trying to combine 6 separate graphs, all have the same legends so I am trying to have only one legend for the combined graph. I tried using the grc1leg command but it keeps saying "class key not found". I would appreciate some help with this.

    Thank you

  • #2
    I think you need to show your exact syntax. A data example would also help. It is also very likely that there is a much simpler way to get your graph.

    Comment


    • #3
      Hi Nick,

      This is the syntax:
      gr combine 1.gph 2.gph 3.gph 4.gph 5.gph 6.gph
      This gives me a combined graph of 6, however, each of them has a legend. The legend is the same for each so I want only 1 for the whole thing.

      Comment


      • #4
        Use -grc1leg- written by Vince Wiggins. Install it first (if not installed already) by typing in Stata command line -findit grc1leg- and click the link (grc1leg from http://www.stata.com/users/vwiggins) for installation. Once done, the code is simple:

        Code:
        grc1leg graphnames
        Also see the help file for further options.
        Roman

        Comment


        • #5
          #3 Sure, but it's the grc1leg command that isn't working. Otherwise you are not adding any information to #1. I find that often when people are trying to combine several graphs, they have missed the fact that a by() option will do what they want in the first place.

          Comment


          • #6
            Apologies, it was my mistake to overlook that the original poster has already used -grc1leg-. @Vikash arya: You said you used -grc1leg- which is giving you error message but provided the codes for -gr combine- which is a Stata built-in command and a different one than -grc1leg- . Please note, as per the forum rules for posting, you are expected to provide exact commands you used.
            Roman

            Comment


            • #7
              Hi Roman,

              Apologies but I thought that since grc1leg is not working, I might be able to get a way through gr combine command. Anyways, here is the grc1leg command and the error message I get when I run it:

              grc1leg 1.gph 2.gph 3.gph 4.gph 5.gph 6.gph
              class key not found
              class key not found
              class key not found
              class key not found

              r(4010);

              end of do-file

              r(4010);

              Comment


              • #8
                You are still not showing the full commands. It is possible that you used different names to save the graphs and not referring them properly while combining. Without looking at your full codes it is difficult to suggest where the problem started. My wild guess will be your graph names being numeric. It may cause problem. Try with alpha-numeriec names to the graphs i.e. m1, m2 etc. instead numerical names. If doesn't work, please provide your full codes and before anything, Please read through the forum rules.
                Roman

                Comment


                • #9
                  Hi Roman,

                  I used the "play" option on the graphs. I made two graphs without the play option and they seem to combine with the grc1leg command with a single legend. So I guess I will create all the graphs again without the play option and then it should be fine. Thank you for your help.

                  Comment


                  • #10
                    I cant get grc1leg2 to work either. Specifically with two pie charts. I try the command, followed by graph names as confirmed in directory (or by the whole path) it returns "class key not found" repeated four times, exactly like shown above and in https://www.stata.com/statalist/arch.../msg01189.html

                    The program works regularly with other graphs. There are incidentally 4 pies in each of these particular graphs, and I suppose the program cannot find something it is looking for. The graphs use default settings for everything, so it's not a nonstandard colour or something of that sort, and nowhere the string "key" is mentioned. It's not spaces, dashes, underscores or so forth in names, based on few tests

                    grc1leg2 ///
                    "C:\Users\
                    [whole path]\pies_saliva.gph" ///
                    "C:\Users\
                    [same whole path as above]\pies_blood.gph"
                    class key not found
                    class key not found
                    class key not found
                    class key not found
                    r(4010);


                    or

                    graph dir, memory
                    Graph pies_blood pies_saliva

                    grc1leg2 pies_blood pies_saliva
                    class key not found
                    class key not found
                    class key not found
                    class key not found
                    r(4010);



                    If I find time I'll try to recreate the problem with example data. In the meanwhile, suggestions?
                    Many tanks

                    Comment


                    • #11
                      Stata has now fixed the problem reported above that was preventing -graph combine- from working properly in Stata 16 and therefore breaking Vince Wiggins' -grc1leg- and my -grc1leg2-. Inspired by Stata's bug fix, I have updated -grc1leg2- to version 1.31. The updated help file compares and demonstrates three ways of constructing a multi-panel graph with a single legend. The first approach covered is to use the -graph- command with the -by()- and -at()- options. It is often the best choice if the only difference across the panels is the value of a categorical variable, which can serve as the -by()- variable. The second approach is to use Stata's -gr combine- command with approriate options on the component graphs, followed by tweaks with Stata's -graph editor- as necessary. The third way is to use -grc1leg- or my -grc1leg2- command.

                      In addition to allowing all Stata's -graph combine- options, grc1leg2:
                      (a) suppresses the display of the legends on the component graphs;*
                      (b) creates and positions a legend in the combined graph by borrowing the legend previously defined for one of the user-selected component graphs;*
                      (c) optionally suppresses the x-titles or the y-titles of the component graphs, borrowing them for the combined graph;
                      (d) optionally suppresses the main title of the component graphs, borrowing it for the main title of the combined graph;
                      (e) optionally applies fine adjustments to several characteristics of the single legend and/or the borrowed titles;
                      (f) facilitates the calibration and coding of the fine adjustments using the dialog invoked with "db grc1leg2".
                      (g) optionally creates a combined graph with no legends at all.
                      * Capabilities inherited from -grc1leg-.

                      Personally, I find that using the -grc1leg2- dialog is a big help tweaking the multi-panel graph's appearance, saving me time and frustration. Please post bug reports or send them to me here: [email protected].

                      If -search grc1leg2- does not discover the program for you, you can "describe" it with:
                      net describe grc1leg2, from("http://digital.cgdev.org/doc/stata/MO/Misc")
                      or install it with:
                      net install grc1leg2, from("http://digital.cgdev.org/doc/stata/MO/Misc")
                      To download the example DO file into your current working directory, type:
                      net get grc1leg2, from("http://digital.cgdev.org/doc/stata/MO/Misc")



                      Comment


                      • #12
                        See this post for a description of the additions to version 1.60 of -grcileg2-.

                        Comment


                        • #13
                          Suppose a user is on a restricted server where they can't install user-written commands without an act of Congress.
                          Is there a built-in way to do this in recent versions of Stata?

                          Comment


                          • #14
                            paulvonhippel

                            ​​​​​​​The solution in #5 of using a by() option does not entail use of community-contributed commands. More at https://journals.sagepub.com/doi/pdf...36867X20976341

                            Using direct labelling [labeling] rather than a legend does not entail such use either.

                            We have no way of knowing whether either solution applies to #13 as you give no details of the graphs in question, or of the code that created them.

                            Comment


                            • #15
                              Hi everyone,

                              I am facing issues combining 6 graphs (which is already a combined graph of two each). If i combine two of them. They appear okay (somewhat). See below.

                              Click image for larger version

Name:	state_910_cases_death.png
Views:	2
Size:	544.8 KB
ID:	1724083
                              When I combine 6 of them. The combine graph is messed up (see below). Nick Cox Andrew Musau do you have any suggestions? (data and code below) Another thing that are not in sync are the dates between the twoway bar graph and heatplot (from scc).
                              Click image for larger version

Name:	state__cases_death.png
Views:	1
Size:	1.34 MB
ID:	1724084


                              Code:
                              * Example generated by -dataex-. For more info, type help dataex
                              clear
                              input float(date1 lgd_state_id) long state1 float(stringency_index cases_pop deaths_pop_mil)
                              21976 10 1  8.33           .           .
                              21977 10 1  8.33           .           .
                              21978 10 1  8.33           .           .
                              21979 10 1 13.89           .           .
                              21980 10 1    25           .           .
                              21981 10 1    25           .           .
                              21982 10 1    25           .           .
                              21983 10 1    25           .           .
                              21984 10 1    25           .           .
                              21985 10 1    25           .           .
                              21986 10 1    25           .           .
                              21987 10 1 44.44           .           .
                              21988 10 1 44.44           .           .
                              21989 10 1 44.44           .           .
                              21990 10 1 44.44           .           .
                              21991 10 1 44.44           .           .
                              21992 10 1 44.44           .           .
                              21993 10 1 44.44           .           .
                              21994 10 1 44.44           .           .
                              21995 10 1 44.44           .           .
                              21996 10 1 85.65 .0016487774           0
                              21997 10 1 85.65  .002473166           0
                              21998 10 1 85.65  .002473166           0
                              21999 10 1   100  .003297555           0
                              22000 10 1   100  .005770721           0
                              22001 10 1   100  .007419499           0
                              22002 10 1   100  .009068276           0
                              22003 10 1   100   .01236583 -.016487774
                              22004 10 1   100   .01236583 -.016487774
                              22005 10 1   100  .017312163 -.016487774
                              22006 10 1   100   .01978533 -.016487774
                              22007 10 1   100   .02390727 -.016487774
                              22008 10 1   100   .02555605 -.016487774
                              22009 10 1   100   .02638044 -.016487774
                              22010 10 1   100   .02638044 -.016487774
                              22011 10 1   100   .02638044 -.016487774
                              22012 10 1   100   .03132677 -.016487774
                              22013 10 1   100   .03215116 -.016487774
                              22014 10 1   100   .04781454 -.016487774
                              22015 10 1   100   .04946332 -.016487774
                              22016 10 1   100   .05276088 -.016487774
                              22017 10 1   100   .05276088 -.016487774
                              22018 10 1   100   .05440966 -.016487774
                              22019 10 1   100   .05440966 -.016487774
                              22020 10 1   100   .05935599 -.016487774
                              22021 10 1   100   .06842426 -.016487774
                              22022 10 1   100   .07007304 -.016487774
                              22023 10 1   100   .07089743 -.032975547
                              22024 10 1   100   .07914132 -.032975547
                              22025 10 1   100   .09315593 -.032975547
                              22026 10 1   100   .10387298 -.032975547
                              22027 10 1   100   .11788759 -.032975547
                              22028 10 1   100   .14014608 -.032975547
                              22029 10 1   100    .1838387 -.032975547
                              22030 10 1   100   .20692156 -.032975547
                              22031 10 1   100   .22835568 -.032975547
                              22032 10 1   100    .2852385 -.032975547
                              22033 10 1   100    .3017263 -.032975547
                              22034 10 1   100    .3322287 -.032975547
                              22035 10 1   100    .3503652 -.032975547
                              22036 10 1   100    .3841651  -.04946332
                              22037 10 1   100     .396531 -.065951094
                              22038 10 1   100     .426209 -.065951094
                              22039 10 1 90.28   .43527725 -.065951094
                              22040 10 1 90.28     .441048 -.065951094
                              22041 10 1 90.28    .4468187 -.065951094
                              22042 10 1 90.28    .4534138  -.08243887
                              22043 10 1 90.28    .4773211  -.08243887
                              22044 10 1 90.28    .5037015  -.08243887
                              22045 10 1 90.28    .5828428  -.09892665
                              22046 10 1 90.28    .6174672  -.09892665
                              22047 10 1 90.28    .7246377  -.09892665
                              22048 10 1 90.28    .7856424  -.11541442
                              22049 10 1 90.28    .8235644  -.11541442
                              22050 10 1 90.28    .8515936  -.11541442
                              22051 10 1 90.28    .9711299  -.13190219
                              22052 10 1 90.28    1.088193  -.13190219
                              22053 10 1 90.28   1.1731051  -.14838997
                              22054 10 1 91.67   1.2522465  -.14838997
                              22055 10 1 91.67   1.4641143  -.14838997
                              22056 10 1 91.67   1.6380603  -.14838997
                              22057 10 1 91.67    1.785626   -.1813655
                              22058 10 1 91.67   1.9735866   -.1813655
                              22059 10 1 91.67   2.1219766   -.1813655
                              22060 10 1 91.67    2.256352  -.21434106
                              22061 10 1 91.67    2.446786  -.23082884
                              22062 10 1 91.67    2.502844   -.2473166
                              22063 10 1 91.67    2.625678   -.2473166
                              22064 10 1 91.67    2.769122   -.2473166
                              22065 10 1 91.67    2.938946   -.3462433
                              22066 10 1 91.67    3.138448   -.3792188
                              22067 10 1 82.41   3.2522135   -.3792188
                              22068 10 1 82.41    3.376696   -.3957066
                              22069 10 1 82.41   3.5663056   -.4121944
                              22070 10 1 82.41    3.670179   -.4616577
                              22071 10 1 82.41    3.790539  -.47814545
                              22072 10 1 82.41    3.982622  -.47814545
                              22073 10 1 82.41    4.179651   -.4946332
                              22074 10 1 82.41   4.3255677    -.511121
                              22075 10 1 82.41   4.4970403   -.5440965
                              end
                              label values state1 state1
                              label def state1 1 "Bihar", modify

                              Code:
                              ********************************************************************************
                              summ date if date1>=21976
                                local x1 = `r(min)'
                                local x2 = `r(max)'
                                *10 19 19 27 32 90*
                                foreach s in 9 10 19 19 27 32 90  {
                              
                                  heatplot stringency_index i.state1 date if lgd_state_id==`s' & date1>=21976, ///
                                   yscale(noline) ///
                                  ylabel(., nogrid labsize(tiny)) ///
                                   xlabel(`x1'(30)`x2', labsize(*0.7) angle(45) format(%tdDD-Mon-yy) nogrid) ///
                                   color(inferno, reverse) ///   
                                   cuts(0(10)100) ///
                                    ramp(bottom space(4) label(0(10)100) ) ///
                                    p(lcolor(gs6) lwidth(*0.05)) ///
                                   ytitle("`s'", size(vsmall) angle(180)) ///
                                   xtitle("", size(vsmall)) ///
                                   fysize(25) 
                                  
                                  
                                  graph save "Graph"  "${graphfolder}/stringency_`s'.gph", replace
                                  
                              
                                  twoway (bar cases_pop  date, fcolor(dknavy) lcolor(dknavy)) ///
                                  (bar deaths_pop_mil date,fcolor(red%40) lcolor(red%60)) ///
                                  if lgd_state_id==`s' & date1>=21976, ///
                                   xtitle(, size(small)) xline(21998 22020 22039  22053,  ///
                                   lwidth(vthin) lpattern(shortdash) lcolor(black)) ///
                                    xtitle("",  size(small)) xline(22067 22097 22128 22159 22189 22220 22250,  ///
                                  lwidth(medthin) lpattern(shortdash_dot_dot) lcolor(maroon)) ///
                                   xlabel("", labsize(*0.6) angle(vertical) format(%tdDD-Mon-yy) nogrid) subtitle (`s') ///
                                    fysize(75) ///
                                   legend(order(1 "Cases per 100k" 2 "Deaths per 2m") position(12) ring(0) cols(2))
                                   
                                       graph save "Graph"  "${graphfolder}/cases_deaths_stringency_`s'.gph", replace
                                      
                                  gr combine "${graphfolder}/cases_deaths_stringency_`s'.gph" ///
                                  "${graphfolder}/stringency_`s'.gph", rows(2)   imargin(0 0 0 0) iscale(0.7) 
                                  
                                  graph save "Graph"  "${graphfolder}/state_`s'_cases_death.gph", replace
                                  
                                  }
                              
                                      
                                  gr combine "${graphfolder}/state_9_cases_death.gph" "${graphfolder}/state_10_cases_death.gph", ///
                                    iscale(0.5) 
                                    graph save "Graph"  "${graphfolder}/state_910_cases_death.gph", replace
                                    graph export "${graphfolder}/state_910_cases_death.png", wid(2500) replace
                                    
                                  gr combine "${graphfolder}/state_19_cases_death.gph" "${graphfolder}/state_27_cases_death.gph", ///
                                    iscale(0.5) 
                                    graph save "Graph"  "${graphfolder}/state_1927_cases_death.gph", replace
                                    
                                  gr combine "${graphfolder}/state_32_cases_death.gph" "${graphfolder}/state_90_cases_death.gph", ///
                                    iscale(0.5) 
                                    graph save "Graph"  "${graphfolder}/state_3290_cases_death.gph", replace
                                    
                                  gr combine "${graphfolder}/state_910_cases_death.gph" "${graphfolder}/state_1927_cases_death.gph" ///
                                  "${graphfolder}/state_3290_cases_death.gph", rows(3) ysize(30) xsize(16)
                                  graph export "${graphfolder}/state__cases_death.png", wid(2500) replace
                              Attached Files

                              Comment

                              Working...
                              X