Announcement

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

  • Bar Graph with loop

    Hi,

    I want to create a bar graph for multiple variables and combine them in a graph using a loop. But my code only generates a bar graph for the first variable. How can I adjust my code to fix this problem?

    My code is:

    foreach var of varlist merge1 merge2 merge3 {
    foreach var of varlist Beschäftigungsanteil_1 Beschäftigungsanteil_2 Beschäftigungsanteil_3 Beschäftigungsanteil_4 {
    graph bar, over (ˋvar') saving (Stellendaten2, replace)
    graph bar, over (ˋvar') saving (BHP2, replace)
    graph bar, over (ˋvar') saving (Beide2, replace)
    graph combine Stellendaten2.gph BHP2.gph Beide2.gph, xcommon ycommon rows(1)
    graph export ˋvar'bar.png
    }
    }

  • #2
    You are naming 3 variables in the outer loop and 4 in the inner loop : that is 4 x 3 = 12 pairs, but

    1. The same local macro var is used for both loops.

    2. Each graph command names only one variable.


    How many graphs do you want to draw -- for which variables?

    Comment


    • #3
      Your code is messy. In the first instance

      foreach var of varlist merge1 merge2 merge3 {
      foreach var of varlist Beschäftigungsanteil_1 Beschäftigungsanteil_2 Beschäftigungsanteil_3 Beschäftigungsanteil_4 {
      `var' is ambiguous, so you need something like

      Code:
      foreach v of varlist merge1 merge2 merge3 {
      foreach var of varlist Beschäftigungsanteil_1 Beschäftigungsanteil_2 Beschäftigungsanteil_3 Beschäftigungsanteil_4 {
      so that you can distinguish `v' and `var'.


      graph bar, over (ˋvar') saving (Stellendaten2, replace)
      graph bar, over (ˋvar') saving (BHP2, replace)
      graph bar, over (ˋvar') saving (Beide2, replace)
      If you are looping and naming the stored graphs the same, you are overwriting the previously stored graphs. I do not know what you ultimately want to suggest how you would go about writing the loop, so my advise is to show this for one instance without using a loop and then explain what is needed.

      Comment


      • #4
        Thanks, Nick, for trying to help. I want to create a graph for the four variables in the inner loop for each variable in the outer loop. I want to compare the values of the variables in the inner loop for each merge1, merge2, merge3 and merge4.

        Originally posted by Nick Cox View Post
        You are naming 3 variables in the outer loop and 4 in the inner loop : that is 4 x 3 = 12 pairs, but

        1. The same local macro var is used for both loops.

        2. Each graph command names only one variable.


        How many graphs do you want to draw -- for which variables?

        Comment


        • #5
          Do you mean for each distinct value of merge1 and so forth?

          This is one of many possible guesses at what you want. Perhaps it is best to work out what graphs you want first before trying to save them anywhere.

          Code:
          forval i = 1/3 {
          forval j = 1/4 { 
          graph bar, over(Beschäftigungsanteil_ˋj') by(merge`i') name(G`i'_`j', replace)
          }
          }
          Note that while Stellendaten2 BHP2 Beide2 no doubt make perfect sense to you, they bear no obvious relation to any of the variable names you've used, which makes this very hard for readers to follow.

          Comment


          • #6
            Thanks for your reply, Nick!

            I tried to run the code you‘ve posted, but I got the response from Stata that the option by () is not allowed. What should I do?

            Originally posted by Nick Cox View Post
            Do you mean for each distinct value of merge1 and so forth?

            This is one of many possible guesses at what you want. Perhaps it is best to work out what graphs you want first before trying to save them anywhere.

            Code:
            forval i = 1/3 {
            forval j = 1/4 {
            graph bar, over(Beschäftigungsanteil_ˋj') by(merge`i') name(G`i'_`j', replace)
            }
            }
            Note that while Stellendaten2 BHP2 Beide2 no doubt make perfect sense to you, they bear no obvious relation to any of the variable names you've used, which makes this very hard for readers to follow.

            Comment


            • #7
              The help explains that by() is an allowed option, so I am at a loss to advise unless you copy and paste the exact syntax you use. A space between by and its argument is not advised.

              Comment


              • #8
                I have now used the more simple code:

                foreach var of varlist Beschäftigungsanteil_1 Beschäftigungsanteil_2 Beschäftigungsanteil_3 Beschäftigungsanteil_4{
                graph bar ˋvar' by(ˋ_merge')
                }

                I want to create a graph, which shows the value of the Beschäftigungsanteil (German word for employment share) for the 3 values of the categorical variable _merge next to each other in one graph. But after running the code written above, I only get a graph showing one bar for the the value of the variable "Beschäftigungsanteil" instead of 3 Bars, which are each showing the value of the variable "Beschäftigungsanteil" for one of the categories of the variable _merge.

                How can I solve this problem?
                Thanks



                Originally posted by Nick Cox View Post
                The help explains that by() is an allowed option, so I am at a loss to advise unless you copy and paste the exact syntax you use. A space between by and its argument is not advised.

                Comment


                • #9
                  I have now solved the problem and could create the graph. Thanks again for trying to help.

                  Comment


                  • #10
                    Your code in #8 was wrong, so being simpler is no virtue.

                    The problems start with needing a comma before by() and presumably continue unless you have defined _merge as a local macro.

                    Solving your problem is understandably what you care about most but "I have now solved the problem" is a report that doesn't help anybody else who comes to the thread in future because the title makes it seem likely to be interesting or useful.

                    Similarly in trying to get help, it doesn't help you or us if "I tried to run the code you‘ve posted" turns out to have meant "I decided to try quite different code, which I am not showing you".

                    Pleased this worked out for you, but in any future thread following detailed advice in the FAQ will get solutions for you faster and more directly.

                    Comment

                    Working...
                    X