Announcement

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

  • Color of bar graph

    Dear all,

    I want to create a bar graph with some conditions below:
    • there is no space between the bars
    • all bars have the same color
    • there is no box for the x-title.
    Unfortunately, I cannot find a solution. I use the below command but it gives me the figure attached. Could anyone know the solution? Many thanks for considering my request.


    clear
    input y x
    1 1 1
    2 2 2
    3 4 3
    4 6 4
    5 9 5
    6 10 6
    7 11 10
    end

    graph bar (asis) y, over(x) asyvars
    Click image for larger version

Name:	Graph.PNG
Views:	1
Size:	26.3 KB
ID:	1705500



  • #2
    Code:
    graph bar (asis) y, over(x)
    And are you sure you want that there is no space between the bars?

    Comment


    • #3
      Hi,

      No, I do not want any space between the bars. I want the bars just next to each other like in the graph but with the same color.

      Comment


      • #4
        Code:
        graph bar (asis) y, over(x, gap(0))

        Comment


        • #5
          Originally posted by Chen Samulsion View Post
          Code:
          graph bar (asis) y, over(x, gap(0))
          many thanks for your help. It works perfectly. I have another question would like to ask. Assume that I have another variable name z. I want to adjust the width of each bar based on the corresponding z value. I would like to ask if you have any suggestions for this. Thank you again.

          Comment


          • #6
            Hi, the bar width could be adjusted by setting option of over(varname, gap([*]#) or bargap(#). However, you'd better provide data example like #1, that will help users to answer your question.

            Comment


            • #7
              Originally posted by Chen Samulsion View Post
              Hi, the bar width could be adjusted by setting option of over(varname, gap([*]#) or bargap(#). However, you'd better provide data example like #1, that will help users to answer your question.
              many thanks for your quick answer. Below is the example:

              clear
              input y x z
              1 1 1 2
              2 2 2 4
              3 4 3 5
              4 6 4 7
              5 9 5 8
              6 10 6 1
              7 11 10 3
              end

              I want to produce the same graph as before with the only difference being that the width of each bar will equal the value of z, for example, if the first bar will have a width =1, then the second bar will have a width = 4/2*1, the third bar have a width = 5/2*1 ...

              Comment


              • #8
                I don't quite follow your question. Why set bar gap according to value of z? But you can try this:
                Code:
                clear
                input id y x z
                1 1 1 2
                2 2 2 4
                3 4 3 5
                4 6 4 7
                5 9 5 8
                6 10 6 1
                7 11 10 3
                end
                preserve
                expand z
                bysort id: gen nobs=_n
                replace y=. if nobs!=1
                gen n=_n
                graph bar (asis) y, over(n) name(bar1)
                graph bar (asis) y, over(n, label(nolabels)) name(bar2)
                restore

                Comment


                • #9
                  Originally posted by Chen Samulsion View Post
                  I don't quite follow your question. Why set bar gap according to value of z? But you can try this:
                  Code:
                  clear
                  input id y x z
                  1 1 1 2
                  2 2 2 4
                  3 4 3 5
                  4 6 4 7
                  5 9 5 8
                  6 10 6 1
                  7 11 10 3
                  end
                  preserve
                  expand z
                  bysort id: gen nobs=_n
                  replace y=. if nobs!=1
                  gen n=_n
                  graph bar (asis) y, over(n) name(bar1)
                  graph bar (asis) y, over(n, label(nolabels)) name(bar2)
                  restore
                  thanks a lot for your help. I will try your suggestion.

                  Comment


                  • #10
                    Originally posted by Chen Samulsion View Post
                    I don't quite follow your question. Why set bar gap according to value of z? But you can try this:
                    Code:
                    clear
                    input id y x z
                    1 1 1 2
                    2 2 2 4
                    3 4 3 5
                    4 6 4 7
                    5 9 5 8
                    6 10 6 1
                    7 11 10 3
                    end
                    preserve
                    expand z
                    bysort id: gen nobs=_n
                    replace y=. if nobs!=1
                    gen n=_n
                    graph bar (asis) y, over(n) name(bar1)
                    graph bar (asis) y, over(n, label(nolabels)) name(bar2)
                    restore
                    thanks a lot for your help. I will try your suggestion.

                    Comment


                    • #11
                      See
                      Code:
                      . search spanning , sj
                      
                      Search of official help files, FAQs, Examples, and Stata Journals
                      
                      SJ-15-1 gr0063  . . . . . Stata tip 122: Variable bar widths in two-way graphs
                              . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  B. Jann
                              Q1/15   SJ 15(1):316--318                                (no commands)
                              highlights the bartype(spanning) option of the twoway bar
                              command, an undocumented feature that can be used to produce
                              bars of different widths


                      As the references in that Tip indicate, I use this about once every 5 years, so some of the details below may be redundant.

                      Code:
                      clear
                      input id y x z
                      1 1 1 2
                      2 2 2 4
                      3 4 3 5
                      4 6 4 7
                      5 9 5 8
                      6 10 6 1
                      7 11 10 3
                      end
                      
                      local N = c(N)
                      
                      insobs 1
                      replace id = 0 in L 
                      replace y = y[_n-1] in L 
                      replace z = 0 in L 
                      insobs 1, before(1)
                      replace y = y[2] in 1 
                      
                      gen mid = sum(z) - 0.5 * z 
                      gen top = sum(z)
                      gen bot = top[_n-1]
                      
                      forval j = 1/`N' {
                          local call `call' `=mid[1 + `j']' "`=id[1 + `j']'"
                      }
                      
                      di `"`call'"'
                      
                      list 
                      
                      twoway bar y bot, bartype(spanning) yla(#7, ang(h)) xla(`call', noticks) xtitle(id) fcolor(none)

                      Click image for larger version

Name:	spanning.png
Views:	1
Size:	12.4 KB
ID:	1705753

                      Comment

                      Working...
                      X