Announcement

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

  • Basic question on graph bar

    Hi

    I have the variables A B X Y H Z that represent means of variables and I would like to have them all in one graph as below but after leaving distances between A B & X Y & H Z.

    In other words, I want the figure to show A versus B space X versus Y then space H versus Z.

    Code:
    graph bar A B X Y H Z

  • #2
    Here "versus" seems to mean "next to". (People can't agree otherwise on whether y on vertical versus x on horizontal, or vice versa, is standard. See https://stats.stackexchange.com/ques...-data-analysis)

    I can't think of a way to do this without a different data layout or a switch to twoway.

    In the absent of a data example, this makes some guesses about the flavour of your data. Your real variable names or variable labels may be too long for this to be a good idea, in which case graph hbar or graph dot may work better.

    Code:
    lear 
    input A B X Y H Z 
    1 2 3 4 5 6 
    end 
    
    rename * data#, addnumber 
     
    gen id = 1 
    reshape long data, i(id) j(which) 
    
    label def which 1 A 2 B 3 X 4 Y 5 H 6 Z 
    label val which which 
    
    gen group = ceil(which/2)
    
    set scheme s1color 
    graph bar data, over(which) over(group, relabel(1 " " 2 " " 3 " ")) nofill ytitle(Whatever)

    Click image for larger version

Name:	barwithspaces.png
Views:	1
Size:	12.7 KB
ID:	1697555


    Comment


    • #3
      Hi

      I included an example with raw data.

      The main variable is surp for which I want to visualize the mean per certain periods. The periods are compE compO b0 b6 bt b5 , each is equal to 1 during the period of interest and 0 otherwise.

      Thus, I want to visualize the means of surp for periods compE next to compO, then periods b0 next to b6, then periods bt next to b5. An example of the data is below.

      How can I get something close to the graph in #1 but would be great if the two mean bars are closer to each other. Thanks


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(date surp compE compO b0 b6 bt b5)
      12421                   . 1 0 0 1 0 0
      12422                   . 1 0 0 1 0 0
      12423        -1.591164231 1 0 0 1 0 0
      12424  1.1481634330000001 1 0 0 1 0 0
      12425        -1.023395481 1 0 0 1 0 0
      12428          -.51169774 0 1 0 0 0 1
      12429           .45562673 0 1 0 0 0 1
      12430         1.010769549 0 1 0 0 0 1
      12431        -1.466396279 0 1 0 0 0 1
      12432          .898627528 0 1 0 0 0 1
      12435  .31823284700000004 1 0 0 1 0 0
      12436        -1.341628327 1 0 0 1 0 0
      12437  .12476795200000013 1 0 0 1 0 0
      12438          .318232846 1 0 0 1 0 0
      12439   .7051626339999999 1 0 0 1 0 0
      12442          -.51169774 0 1 0 0 1 1
      12443         -.636465692 0 1 0 0 1 1
      12444  1.0794664900000002 0 1 0 0 1 1
      12445  -.7612336439999999 0 1 0 0 1 0
      12446           .51169774 0 1 0 0 1 0
      12449  -.7051626339999999 0 1 0 0 1 0
      12450         -.318232846 0 1 0 0 1 0
      12451  .19867696699999993 0 1 0 0 1 0
      12452         1.459598087 1 0 1 0 1 0
      12453         -.906839736 1 0 1 0 0 0
      12456                   0 1 0 1 0 0 0
      12457          .950112285 1 0 1 0 0 0
      12458  .26753628500000004 1 0 1 0 0 0
      12459 .043272549999999965 0 1 0 0 0 1
      12460         -1.30419367 0 1 0 0 0 1
      12463          .596030901 0 1 0 0 0 1
      12464        -1.304193669 0 1 0 0 0 1
      12465          .708162768 0 1 0 0 0 1
      12466          1.26092112 1 0 0 1 0 0
      12467        -1.416325537 1 0 0 1 0 0
      12470         1.770406922 1 0 0 1 0 0
      12471 -1.9690838890000002 1 0 0 1 0 0
      12472          .751435318 1 0 0 1 0 0
      12473          .552758352 0 1 0 0 0 1
      12474        -1.105516703 0 1 0 0 0 1
      12477          1.26092112 0 1 0 0 0 1
      12478  -.6648902189999999 0 1 0 0 0 1
      12479          .246365274 0 1 0 0 0 1
      12480          .551541808 1 0 0 1 0 0
      12481 -.14603545500000004 1 0 0 1 0 0
      12484        -1.103083616 1 0 0 1 0 0
      12485 -.29207091100000004 1 0 0 1 0 0
      12486         1.046365895 1 0 0 1 0 0
      12487                   0 0 1 0 0 0 1
      12488          .494824087 0 1 0 0 0 1
      12491          -.90033044 0 1 0 0 1 0
      12492         -.697577263 0 1 0 0 1 0
      12493          .697577263 0 1 0 0 1 0
      12494          .405506353 0 1 0 0 1 0
      12495         -.202753176 0 1 0 0 1 0
      12498                   0 1 0 1 0 1 0
      12499        -1.249119071 1 0 1 0 0 0
      12500         1.597907702 1 0 1 0 0 0
      12501         1.451872248 1 0 1 0 0 0
      12502         -.494824087 1 0 1 0 0 0
      12505         -.957048161 0 1 0 0 0 1
      12506  .20275317699999995 0 1 0 0 0 1
      12507        -1.249119071 0 1 0 0 0 1
      12508          .697577263 0 1 0 0 0 1
      12509         -.348788632 0 1 0 0 0 1
      12512         -.567840132 1 0 0 1 0 0
      12513         1.408354209 1 0 0 1 0 0
      12514         -.205441643 1 0 0 1 0 0
      12515  -.5379319499999999 1 0 0 1 0 0
      12516  .41088328499999993 1 0 0 1 0 0
      12519          .205441642 0 1 0 0 0 1
      12520          -1.0758639 0 1 0 0 0 1
      12521   .9974709230000001 0 1 0 0 0 1
      12522  -.6649806149999999 0 1 0 0 0 1
      12523   .6649806149999999 0 1 0 0 0 1
      12526  .12704866599999998 1 0 0 1 0 0
      12527         -.175704355 1 0 0 1 0 0
      12528         -.489276261 1 0 0 1 0 0
      end
      format %td date

      Comment


      • #4
        This is a different question as you don't have 6 variables showing means, but one outcome for which you want various means. Also, the indicators
        Code:
         
         compE compO b0 b6 bt b5 
        are not mutually exclusive. If you don't mind the overlap of categories, then you can get 6 means on a bar chart as below. gen toshow = . gen which = _n local i = 1 foreach v in compE compO b0 b6 bt b5 { su surp if `v', meanonly replace toshow = r(mean) in `i' label def which `i' "`v'", add local ++i } label val which which gen group = ceil(which/2) set scheme s1color graph bar (asis) toshow, over(which) over(group, relabel(1 " " 2 " " 3 " ")) nofill ytitle(Whatever)

        Comment


        • #5
          Sorry but I think the forum messed up the code a bit in #4. I could not run it in my do file.

          Comment


          • #6
            It wasn't the forum so much as my not putting CODE delimiters everywhere they were needed.


            This is a different question as you don't have 6 variables showing means, but one outcome for which you want various means.

            Also, the indicators
            Code:
             compE compO b0 b6 bt b5 
            are not mutually exclusive.

            If you don't mind the overlap of categories, then you can get 6 means on a bar chart as below.

            Code:
            gen toshow = .
            gen which = _n
            
            local i = 1
            foreach v in compE compO b0 b6 bt b5 {
            su surp if `v', meanonly
            replace toshow = r(mean) in `i'
            label def which `i' "`v'", add
            local ++i
            }
            
            label val which which
            
            gen group = ceil(which/2)
            
            set scheme s1color
            
            graph bar (asis) toshow, over(which) over(group, relabel(1 " " 2 " " 3 " ")) nofill ytitle(Whatever)

            Comment


            • #7

              Thanks. Yes, some of the categories overlap.

              Unfortunately, I ran the code but it has been running since then until I enforced it to stop. I have nearly 7000 daily observations which seem to be the issue. Is there any faster way?

              Comment


              • #8
                That should be computationally trivial even with 7000 observations. But

                Code:
                gen which = _n in 1/6
                may avoid misinterpretation.

                Comment


                • #9
                  Thanks, it works now.
                  Many thanks.

                  Comment


                  • #10
                    One question to the code in #6, how can I show the variables: compE compO b0 b6 bt b5 by their labels. For example, compE is labeled as Decision Month in my data and I want to show this rather than compE?

                    Comment


                    • #11
                      Replace

                      Code:
                      label def which `i' "`v'", add
                      with
                      Code:
                      local label : variable label  `v'  
                      
                      label def which `i' "`label'", add
                      Last edited by Nick Cox; 18 Jan 2023, 01:59.

                      Comment


                      • #12
                        Thanks.
                        I did that in #11 but it gave me the error message "label not allowed" r(101)

                        Comment


                        • #13
                          This works for me:


                          Code:
                          clear
                          input double(date surp compE compO b0 b6 bt b5)
                          12421                   . 1 0 0 1 0 0
                          12422                   . 1 0 0 1 0 0
                          12423        -1.591164231 1 0 0 1 0 0
                          12424  1.1481634330000001 1 0 0 1 0 0
                          12425        -1.023395481 1 0 0 1 0 0
                          12428          -.51169774 0 1 0 0 0 1
                          12429           .45562673 0 1 0 0 0 1
                          12430         1.010769549 0 1 0 0 0 1
                          12431        -1.466396279 0 1 0 0 0 1
                          12432          .898627528 0 1 0 0 0 1
                          12435  .31823284700000004 1 0 0 1 0 0
                          12436        -1.341628327 1 0 0 1 0 0
                          12437  .12476795200000013 1 0 0 1 0 0
                          12438          .318232846 1 0 0 1 0 0
                          12439   .7051626339999999 1 0 0 1 0 0
                          12442          -.51169774 0 1 0 0 1 1
                          12443         -.636465692 0 1 0 0 1 1
                          12444  1.0794664900000002 0 1 0 0 1 1
                          12445  -.7612336439999999 0 1 0 0 1 0
                          12446           .51169774 0 1 0 0 1 0
                          12449  -.7051626339999999 0 1 0 0 1 0
                          12450         -.318232846 0 1 0 0 1 0
                          12451  .19867696699999993 0 1 0 0 1 0
                          12452         1.459598087 1 0 1 0 1 0
                          12453         -.906839736 1 0 1 0 0 0
                          12456                   0 1 0 1 0 0 0
                          12457          .950112285 1 0 1 0 0 0
                          12458  .26753628500000004 1 0 1 0 0 0
                          12459 .043272549999999965 0 1 0 0 0 1
                          12460         -1.30419367 0 1 0 0 0 1
                          12463          .596030901 0 1 0 0 0 1
                          12464        -1.304193669 0 1 0 0 0 1
                          12465          .708162768 0 1 0 0 0 1
                          12466          1.26092112 1 0 0 1 0 0
                          12467        -1.416325537 1 0 0 1 0 0
                          12470         1.770406922 1 0 0 1 0 0
                          12471 -1.9690838890000002 1 0 0 1 0 0
                          12472          .751435318 1 0 0 1 0 0
                          12473          .552758352 0 1 0 0 0 1
                          12474        -1.105516703 0 1 0 0 0 1
                          12477          1.26092112 0 1 0 0 0 1
                          12478  -.6648902189999999 0 1 0 0 0 1
                          12479          .246365274 0 1 0 0 0 1
                          12480          .551541808 1 0 0 1 0 0
                          12481 -.14603545500000004 1 0 0 1 0 0
                          12484        -1.103083616 1 0 0 1 0 0
                          12485 -.29207091100000004 1 0 0 1 0 0
                          12486         1.046365895 1 0 0 1 0 0
                          12487                   0 0 1 0 0 0 1
                          12488          .494824087 0 1 0 0 0 1
                          12491          -.90033044 0 1 0 0 1 0
                          12492         -.697577263 0 1 0 0 1 0
                          12493          .697577263 0 1 0 0 1 0
                          12494          .405506353 0 1 0 0 1 0
                          12495         -.202753176 0 1 0 0 1 0
                          12498                   0 1 0 1 0 1 0
                          12499        -1.249119071 1 0 1 0 0 0
                          12500         1.597907702 1 0 1 0 0 0
                          12501         1.451872248 1 0 1 0 0 0
                          12502         -.494824087 1 0 1 0 0 0
                          12505         -.957048161 0 1 0 0 0 1
                          12506  .20275317699999995 0 1 0 0 0 1
                          12507        -1.249119071 0 1 0 0 0 1
                          12508          .697577263 0 1 0 0 0 1
                          12509         -.348788632 0 1 0 0 0 1
                          12512         -.567840132 1 0 0 1 0 0
                          12513         1.408354209 1 0 0 1 0 0
                          12514         -.205441643 1 0 0 1 0 0
                          12515  -.5379319499999999 1 0 0 1 0 0
                          12516  .41088328499999993 1 0 0 1 0 0
                          12519          .205441642 0 1 0 0 0 1
                          12520          -1.0758639 0 1 0 0 0 1
                          12521   .9974709230000001 0 1 0 0 0 1
                          12522  -.6649806149999999 0 1 0 0 0 1
                          12523   .6649806149999999 0 1 0 0 0 1
                          12526  .12704866599999998 1 0 0 1 0 0
                          12527         -.175704355 1 0 0 1 0 0
                          12528         -.489276261 1 0 0 1 0 0
                          end
                          format %td date
                          
                          gen toshow = .
                          gen which = _n in 1/6 
                          local i = 1 
                          label var compE "some"
                          label var compO "enchanted"
                          label var b0 "evening"
                          label var b6 "you will"
                          label var bt "meet a"
                          label var b5 "stranger"
                          
                          foreach v in compE compO b0 b6 bt b5 { 
                              su surp if `v', meanonly 
                              replace toshow = r(mean) in `i'
                              local label : variable label `v'
                              label def which `i' "`label'", add 
                              local ++i 
                              
                          }
                          
                          label val which which 
                          gen group = ceil(which/2)
                          set scheme s1color 
                          graph bar (asis) toshow, over(which) over(group, relabel(1 " " 2 " " 3 " ")) nofill ytitle(Whatever)
                          Click image for larger version

Name:	worksforme.png
Views:	1
Size:	13.8 KB
ID:	1697717

                          Comment


                          • #14
                            Should be

                            Some enchanted evening
                            You may see a stranger

                            Comment

                            Working...
                            X