Announcement

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

  • Graph title looping over 2 variables

    Hello.

    I am trying to create graphs that loop over a variable (mem_id ). But I would also like to include the value of another string variable- the group to which the member belongs- in the title.
    So it would look like:
    Group X
    Member 1

    The alternatives I tried so far did not work.

    The basic code I am using is this:

    levelsof mem_id, local(levels)
    foreach l of local levels {
    twoway (bar varx week , sort) ylabel(, labsize(vsmall)) ytitle("") xlabel(, labsize(vsmall)) xtitle("") title("Member `l'") legend (off)
    graph export "`l'.png", replace
    }

    Any suggestions on how to do this?

    Thank you very much.

    Laura


    Regards,
    Laura Cojocaru

  • #2
    I could try to imagine what your data looks like and give you some code that would work for my imaginary data set. But that would probably just waste my time writing it and yours reading it and trying it only to find out it's not appropriate.

    So please post back showing example data. Do that using the -dataex- command. (See FAQ #12 if you are not familiar with -dataex-) That way it will be possible to write code that could work with your data, and test it before passing it on to you.

    Comment


    • #3
      Indeed I never heard of dataex before, but thank you, I learned something new.

      Below is the output:

      Code:
      * Example generated by -dataex-. To    install: ssc install dataex
      clear
      input float mem_id str13 group_name    float(week tot_mem_week_deposits)
      22 "PONGEZI"       2961 12000
      87 "MSHIKAMANO"    2988 10000
      106 "WEKA AKIBA"    2988 12000
      193 "ZINDUKA"       2982 20000
      194 "MAKAZI KWANZA" 2965 20000
      9 "JIENDELEZE"    2997 18000
      23 "JIENDELEZE"    3016  8000
      192 "NEEMA"         3010 10000
      196 "NEEMA"         3007 20000
      247 "ATUKUTEGEMEA"  3001  5000
      end
      My current code is:

      levelsof mem_id, local(levels)
      foreach l of local levels {
      twoway (bar tot_mem_week_deposits week , sort) ylabel(, labsize(vsmall)) ytitle("") xlabel(, labsize(vsmall)) xtitle("") title("Member `l'") legend (off)
      graph export "`l'.png", replace
      }

      I would like the chart title to be for example
      Member 22
      PONGEZI

      Many thanks again.
      Regards,
      Laura Cojocaru

      Comment


      • #4
        I think this does it:

        Code:
        * Example generated by -dataex-. To    install: ssc install dataex
        clear
        input float mem_id str13 group_name    float(week tot_mem_week_deposits)
        22 "PONGEZI"       2961 12000
        87 "MSHIKAMANO"    2988 10000
        106 "WEKA AKIBA"    2988 12000
        193 "ZINDUKA"       2982 20000
        194 "MAKAZI KWANZA" 2965 20000
        9 "JIENDELEZE"    2997 18000
        23 "JIENDELEZE"    3016  8000
        192 "NEEMA"         3010 10000
        196 "NEEMA"         3007 20000
        247 "ATUKUTEGEMEA"  3001  5000
        end
        
        levelsof mem_id, local(levels)
        gen long obs_no = _n 
        foreach l of local levels { 
            // FIND GROUP NAME FOR THIS MEMBER
            summ obs_no if mem_id == `l', meanonly
            local group = group_name[`r(min)']
            
            twoway (bar tot_mem_week_deposits week) ,  ylabel(, labsize(vsmall)) ///
                ytitle("") xlabel(, labsize(vsmall)) xtitle("") ///
                title("Member `l'" "`group'") legend (off)
            graph export "`l'.png", replace
        }
        Note: The trick is to identify the group name associated with the member_id. Since group name is a string variable, we have to actually first identify the observation number(s) that contain(s) the member_id and pull the group name from there. Then we store the result in a local macro, and use the local macro in the -title()- option.

        Also, I think you modified this code from something that actually ran. There is no -sort- option permitted with twoway bar; that option is for -twoway line-. In general, it is not a good idea to show code that is not what you are really doing--you could get incorrect advice. Or, somebody trying to help you could see that the code doesn't run anyway and might just choose not to look into it.

        Also, I note that your loop does not produce separate graphs for each member id. Even though the graph command is embedded in a loop over member id's, the graph command does not select out the observations pertaining to the current member: it's just graphing from the entire data set. I think you want to take -if mem_id == `l'- onto that command.

        Comment


        • #5
          Thank you for the help and the clear explanation. It worked perfectly.

          Yes, you are right, I simplified the actual code which included multiple variables on the same graph, leaving out the if mem_id == `l' by mistake.

          My current code does have sort too. Do you mean that it is unnecessary? Because it is permitted, unless I am missing something. I just tested it again from the Graphics menu.
          twoway (bar tot_mem_week_deposits week, sort) works

          Thank you again.
          Regards,
          Laura Cojocaru

          Comment


          • #6
            Oh, yes, I see. The -sort- option is needed if you want the bars sorted properly. I had marked up your code a bit and got an error for the sort option, but that was actually because I had it outside the parentheses instead of inside--so it's allowed, just I had it in the wrong place. Sorry for any confusion about that.

            Comment

            Working...
            X