Announcement

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

  • Change axis in graph bar

    Hi Statausers,

    I think is a common issue, but I couldn't find the answer yet. I have the following graph with a variable that ranges from 1 to 10. However I would like to change the y-axis to just show for example from 6 to 10. How can I do that?

    Options like

    Code:
    graph bar (mean) score, over(country, label(labsize(vsmall))) over(date) yscale(range(6 10)) ylabel(6(1)10)
    aren't working.

    Cheers!
    Click image for larger version

Name:	graph.png
Views:	1
Size:	154.2 KB
ID:	1725255


  • #2
    There is a way which I will not mention because it is a bad idea. See #2 https://www.statalist.org/forums/for...-for-bar-chart which recommends graph dot instead.

    Code:
    help graph dot

    Comment


    • #3
      Thanks for the answer. So, first, I don't get what's the harm in a case like the one that I'm describing, in truncating or changing the axis. Second, using graph dot, still I can produce a similar output. This is my new code.

      Code:
      graph dot (mean) score, over(guest_country_code, label(labsize(vsmall))) over(match_date) yscale(range(6 10)) ylabel(6(1)10) vertical
      which leads to:

      Click image for larger version

Name:	Graph.png
Views:	1
Size:	282.9 KB
ID:	1725344



      Thanks !!

      Comment


      • #4
        Originally posted by Jean Jacques View Post
        So, first, I don't get what's the harm in a case like the one that I'm describing, in truncating or changing the axis.
        One issue is that starting a bar chart at some arbitrary positive non-zero value can lead to misleading visual interpretations. Note that a bar represents some quantity, either "how much" (such as a numerical measurement) or "how many" (such as a count or frequency).

        second, using graph dot, still I can produce a similar output.
        Please provide a data example using the dataex command.

        Comment


        • #5
          Thanks for the answer. Agree with the first point. Indeed you can see many misleading graphs with different axis. Although in some cases (as the one I showed above) a simple bar chart is non-informative at all (and in this case are not quantities, are means).

          About the second, here you have. Thanks

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double score byte guest_country_code long match_date
           7 0 0
          10 1 0
           9 1 0
          10 0 0
           7 1 0
          10 1 0
          10 1 0
           6 1 0
           5 0 0
          10 1 0
           8 1 0
           6 1 0
           8 1 0
           6 1 0
           5 0 0
           8 1 0
          10 0 0
           9 1 0
          10 0 0
           9 1 0
           7 0 0
          10 1 0
           5 0 1
          10 0 1
           8 1 1
          10 1 1
          10 1 2
           7 0 2
           7 1 2
           7 0 2
          end
          label values guest_country_code country_label
          label def country_label 0 "IT", modify
          label def country_label 1 "UK", modify

          Comment


          • #6
            graph dot with the exclude0 option is ideal for your circumstances.

            Comment


            • #7
              Great thanks. One more question. How can I make that the dots display a different color accoridig to whether they belong to a different guest_country_code (which is a dummy taking 0 or 1).

              Up to now I do this:

              Code:
              graph dot (mean) score, over(guest_country_code, label(labsize(vsmall))) over(match_date) yscale(range(6 10)) ylabel(6(1)10) vertical exclude0  dots(msymbol(p))  marker(1, mcolor(green)) marker(2, mcolor(red))
              without success. Thanks
              Click image for larger version

Name:	graph.png
Views:	1
Size:	145.1 KB
ID:	1725396

              Comment


              • #8
                Option asyvars should help.

                Comment


                • #9
                  Thanks Nick, it worked. How should I proceed if I want to add confidence intervals? dot graph doesn't seems to accept it. Should I move to a two-way scatter? Would be possible to do that keeping the original chart as the one I have below:

                  Code:
                  graph dot (mean) score, over(guest_country_code, label(labsize(vsmall))) over(match_date, label(angle(45) labsize(small)) relabel(1 "- 5 days" 2 "- 4 days" 3 "- 3 days" 4 "- 2 days" 5 "- 1 day" 6 "Game day" 7 "+ 1 day" 8 "+ 2 day" 9 "+ 3 days" 10 "+ 4 days" 11 "+ 5 days" 12 "+ 6 days")) yscale(range(6 10)) ylabel(6(1)10) vertical exclude0  dots(msymbol(p)) asyvars marker(1, mcolor(gs9) msymbol(S)) marker(2, mcolor(gs3) msymbol(O)) ytitle("Average score")
                  Thanks
                  Jean

                  Comment


                  • #10
                    See https://www.statalist.org/forums/for...ith-error-bars with a recent thread with a focus on error bars.

                    Comment


                    • #11
                      Sorry Nick, but I can still figure it out and I've been spending a good couple of hours on this.

                      I have computed the intervals

                      Code:
                      gen lb = .
                      gen ub = .
                      
                      foreach c in "0" "1"{
                      foreach x in "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" {
                      ci means score if (match_date == `x') & (guest_country_code == `c')
                      replace lb = `r(lb)' if (match_date == `x') & (guest_country_code == `c')
                      replace ub = `r(ub)' if (match_date == `x') & (guest_country_code == `c')
                      }
                      }
                      but then I can replicate something similar to what you did in the other thread.

                      Here you have a snippet of my dataset.

                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input double score byte guest_country_code long match_date float(lb ub)
                       7 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       8 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       7 1 1 7.996719 8.167284
                       5 0 1  8.22762 8.408016
                      10 1 1 7.996719 8.167284
                       8 0 1  8.22762 8.408016
                      10 0 1  8.22762 8.408016
                      10 0 1  8.22762 8.408016
                       5 1 1 7.996719 8.167284
                       6 1 1 7.996719 8.167284
                       8 0 1  8.22762 8.408016
                       8 1 1 7.996719 8.167284
                      10 0 1  8.22762 8.408016
                       8 1 1 7.996719 8.167284
                       9 1 1 7.996719 8.167284
                       8 1 1 7.996719 8.167284
                       8 0 1  8.22762 8.408016
                       7 1 1 7.996719 8.167284
                      10 0 1  8.22762 8.408016
                      10 0 1  8.22762 8.408016
                       9 0 1  8.22762 8.408016
                       9 1 1 7.996719 8.167284
                       5 1 1 7.996719 8.167284
                       8 0 1  8.22762 8.408016
                      10 1 1 7.996719 8.167284
                       3 1 1 7.996719 8.167284
                      10 0 1  8.22762 8.408016
                       7 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       6 0 1  8.22762 8.408016
                       7 1 1 7.996719 8.167284
                       9 1 1 7.996719 8.167284
                       8 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       8 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       8 1 1 7.996719 8.167284
                       6 0 1  8.22762 8.408016
                       9 0 1  8.22762 8.408016
                       8 1 1 7.996719 8.167284
                       7 0 1  8.22762 8.408016
                       8 0 1  8.22762 8.408016
                      10 1 1 7.996719 8.167284
                       8 0 1  8.22762 8.408016
                       5 1 1 7.996719 8.167284
                       9 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       8 0 1  8.22762 8.408016
                      10 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                       7 0 1  8.22762 8.408016
                      10 0 1  8.22762 8.408016
                      10 1 1 7.996719 8.167284
                      10 0 1  8.22762 8.408016
                       8 0 1  8.22762 8.408016
                      10 0 1  8.22762 8.408016
                       8 0 1  8.22762 8.408016
                       5 1 1 7.996719 8.167284
                       8 1 1 7.996719 8.167284
                       9 1 1 7.996719 8.167284
                      10 1 1 7.996719 8.167284
                      10 0 1  8.22762 8.408016
                       6 1 1 7.996719 8.167284
                       6 0 1  8.22762 8.408016
                      10 1 1 7.996719 8.167284
                       7 1 1 7.996719 8.167284
                      end
                      label values guest_country_code country_label
                      label def country_label 0 "IT", modify
                      label def country_label 1 "UK", modify

                      Can you help me?

                      Comment


                      • #12
                        The graph from this looks fairly silly, but the hope is that the code will work fairly well for the complete dataset.

                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input double score byte guest_country_code long match_date float(lb ub)
                         7 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         8 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         7 1 1 7.996719 8.167284
                         5 0 1  8.22762 8.408016
                        10 1 1 7.996719 8.167284
                         8 0 1  8.22762 8.408016
                        10 0 1  8.22762 8.408016
                        10 0 1  8.22762 8.408016
                         5 1 1 7.996719 8.167284
                         6 1 1 7.996719 8.167284
                         8 0 1  8.22762 8.408016
                         8 1 1 7.996719 8.167284
                        10 0 1  8.22762 8.408016
                         8 1 1 7.996719 8.167284
                         9 1 1 7.996719 8.167284
                         8 1 1 7.996719 8.167284
                         8 0 1  8.22762 8.408016
                         7 1 1 7.996719 8.167284
                        10 0 1  8.22762 8.408016
                        10 0 1  8.22762 8.408016
                         9 0 1  8.22762 8.408016
                         9 1 1 7.996719 8.167284
                         5 1 1 7.996719 8.167284
                         8 0 1  8.22762 8.408016
                        10 1 1 7.996719 8.167284
                         3 1 1 7.996719 8.167284
                        10 0 1  8.22762 8.408016
                         7 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         6 0 1  8.22762 8.408016
                         7 1 1 7.996719 8.167284
                         9 1 1 7.996719 8.167284
                         8 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         8 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         8 1 1 7.996719 8.167284
                         6 0 1  8.22762 8.408016
                         9 0 1  8.22762 8.408016
                         8 1 1 7.996719 8.167284
                         7 0 1  8.22762 8.408016
                         8 0 1  8.22762 8.408016
                        10 1 1 7.996719 8.167284
                         8 0 1  8.22762 8.408016
                         5 1 1 7.996719 8.167284
                         9 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         8 0 1  8.22762 8.408016
                        10 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                         7 0 1  8.22762 8.408016
                        10 0 1  8.22762 8.408016
                        10 1 1 7.996719 8.167284
                        10 0 1  8.22762 8.408016
                         8 0 1  8.22762 8.408016
                        10 0 1  8.22762 8.408016
                         8 0 1  8.22762 8.408016
                         5 1 1 7.996719 8.167284
                         8 1 1 7.996719 8.167284
                         9 1 1 7.996719 8.167284
                        10 1 1 7.996719 8.167284
                        10 0 1  8.22762 8.408016
                         6 1 1 7.996719 8.167284
                         6 0 1  8.22762 8.408016
                        10 1 1 7.996719 8.167284
                         7 1 1 7.996719 8.167284
                        end
                        label values guest_country_code country_label
                        label def country_label 0 "IT", modify
                        label def country_label 1 "UK", modify
                        
                        preserve 
                        
                        statsby, by(guest_country_code match_date) clear: ci means score  
                        
                        * delta is at choice 
                        local delta = 0.2 
                        gen where = match_date + cond(guest_country_code == 1, `delta', -`delta')
                        
                        su match_date, meanonly 
                        local dmin = r(min)
                        local dmax = r(max)
                        
                        forval x = `dmin'/`dmax' { 
                            local left = `x' - `delta'
                            local right = `x' + `delta'
                            local xmla `xmla' `left' "IT" `right' "UK"
                        }
                        
                        scatter mean where, xla(`dmin'/`dmax') || rcap ub lb where, xmla(`xmla', tlc(none) labsize(medsmall)) legend(off) ///
                        ytitle(mean score and 95% confidence interval) xtitle(match date)

                        Comment


                        • #13
                          Great, thanks.

                          Just in case someone else wants to know the code to leave it is what I was looking for, here you have the code:

                          Code:
                          preserve 
                          
                          statsby, by(guest_country_code match_date) clear: ci means score  
                          
                          * delta is at choice 
                          local delta = 0.2 
                          gen where = match_date + cond(guest_country_code == 1, `delta', -`delta')
                          
                          
                          
                          su match_date, meanonly 
                          local dmin = r(min)
                          local dmax = r(max)
                          
                          forval x = `dmin'/`dmax' { 
                              local left = `x' - `delta'
                              local right = `x' + `delta'
                              local xmla `xmla' `left' "IT" `right' "UK"
                          }
                          
                          
                          scatter mean where, xmla(`dmin'/`dmax', labsize(vsmall) angle(90) tlc(none) tlength(*9)) mcolor(gs2) msymbol(s) xmlabel(1 "- 5 days" 2 "- 4 days" 3 "- 3 days" 4 "- 2 days" 5 "- 1 day" 6 "Game day" 7 "+ 1 day" 8 "+ 2 day" 9 "+ 3 days" 10 "+ 4 days" 11 "+ 5 days" 12 "+ 6 days") || rcap ub lb where, lcolor(gs2) xla(`xmla', grid glcolor(gs7) glpattern(dot) labsize(small)) graphregion(color(white)) bgcolor(white) ylab(7(1)10, nogrid) xtitle("") legend(off)

                          Comment

                          Working...
                          X