Announcement

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

  • how to use the stata "bar" to graph a picture like this

    how to use the following data to graph a picture like this
    clear
    input double 系数 str15 行业
    .363 "农业采矿业"
    .203 "制造业"
    .307 "建筑业"
    .501 "低端服务业"
    .6 "高端服务业"
    .543 "社会服务业"
    end
    [/CODE]

    Click image for larger version

Name:	WechatIMG5.jpeg
Views:	1
Size:	128.4 KB
ID:	1557019

  • #2
    This may help:

    Code:
    sysuse auto , clear
    graph bar (asis) mpg in 1/5, over(make, sort(1))

    Comment


    • #3
      What stumbled me in answering this question was how to rotate the labels into vertical orientation (as shown in roy zhong 's post). It seems that Stata doesn't offer that option. The best I could do (see image below) was to rotate the labels to be read sideways, but it is not exactly what was requested. This is because the orientation attribute doesn't seem to be a property of the label style, which only offers {color, size, angle, text gap} according to the Graph Editor.

      Interestingly I didn't see any chart online that would actually direct the labels vertically. All of them seem to be sideways or at an angle, kind of like at this page (no idea what that page is about):
      http://www.kaixian.tv/gd/2018/0914/661697_7.html


      Click image for larger version

Name:	Graph.png
Views:	1
Size:	40.7 KB
ID:	1557100



      PS: Also, while playing with the settings in the Graph Editor, I've noticed occasionally getting a message in the Stata's output window:

      . invalid line in style file anglestyle-ninety:
      *label "90 degrees"


      Which was not shown as error (no red color), but could signify some inconsistencies in the Stata-supplied style files.




      Comment


      • #4
        You can request for vertical labels, no problems...

        Code:
        sysuse auto, clear
        label define repair 1 "very poor" 2 "poor" 3 "medium" 4 good 5 "very good"
        lab values rep78 repair
        gr bar price, over(rep78, sort(1) lab(angle(vertical))) scheme(s1color)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	50.9 KB
ID:	1557107

        Comment


        • #5
          I really can't speak for (let alone in) Chinese but I am reminded of programs from a few decades back that used to show axis titles rendered like this:

          Code:
          S
          o
          m
          e
          t
          h
          i
          n
          g
          
          e
          x
          p
          l
          a
          n
          a
          t
          o
          r
          y
          which no doubt amused the typical programmer if not the typical reader.

          In languages I know, vertical labels on bar charts with vertical bars are usually a sign that you should be thinking of horizontal bars and horizontal text labels.

          All that said, is this what Sergiy Radyakin is seeking?

          Code:
          sysuse auto , clear
          graph bar (asis) mpg in 1/5, over(make, sort(1) label(angle(90)))
          Click image for larger version

Name:	sergei.png
Views:	1
Size:	21.5 KB
ID:	1557116

          Comment


          • #6
            Both pictures of Nick Cox and Andrew Musau show a variation of what I have shown above, achieved with the angle - that rotates the whole label to a certain angle.

            What I was looking for is the vertical text as shown in Nick's 'code' paragraph ("Something explanatory" letter-after-letter in each line). This is known as 'stacked' labels in Excel.

            Is that possible for Stata graphs?

            Click image for larger version

Name:	stacked_labels.png
Views:	1
Size:	32.5 KB
ID:	1557118

            Comment


            • #7
              That's not available in Stata so far as I know. Long may that continue.

              Comment


              • #8
                Probably not a good idea, if Nick says so! But with line breaks, here is a workaround. Would be easier to create the label within the dataset by decoding the variable and looping, using double quotes to enclose each letter.

                Code:
                sysuse auto, clear
                set scheme s1mono
                gr bar price, over(rep78, sort(1) lab(angle(horizontal)) ///
                relabel (1 `""v" "e" "r" "y" " " "p" "o" "o" "r"' ))
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.7 KB
ID:	1557141

                Last edited by Andrew Musau; 05 Jun 2020, 09:38.

                Comment


                • #9
                  Dear Andrew,
                  one problem with this approach is that the indices that you place in the relabel are BAR-numbers, not the original categories.
                  This has been also shown here: https://www.statalist.org/forums/for...or-legend-keys

                  So the code
                  Code:
                      clear all
                      sysuse auto
                      graph bar price if rep78>3, over(rep78, sort(1)  ///
                      relabel (1 `""v" "e" "r" "y" " " "p" "o" "o" "r""' ))
                  Still applies the customized label, even though the category 1 is no longer shown on the graph.

                  So there is an extra step to remap from categories to bar-indices, which makes this whole thing a mess if you want that to be produced automatically.

                  In principle, I think it would be better if we could have the default behavior to fit the label under the bar wrapping the text as a paragraph as the second panel below shows.
                  (obviously completely automatically, manual override is here just to create the desired effect).

                  Coincidentally, this turns out to be the default formatting in Excel charts (shown at the end of this post). I am not advocating to use Excel or reproduce their defaults (which are strange in many cases), but on these simple points I think they've thought about them and got them right, at least for me as a user.

                  The example with Chinese characters could then be done with sufficiently narrow bars, but the usefulness would be far beyond that case.

                  Thank you, Sergiy

                  Click image for larger version

Name:	Graph.png
Views:	1
Size:	82.2 KB
ID:	1557162

                  Code:
                      clear all
                      sysuse auto
                      graph bar price if rep78>3, over(rep78, sort(1)  ///
                      relabel(2 `"very poor, but not as bad as the other guys"' ///
                              1 `"somewhat better, though still far from perfect"')) ///
                      title("Current Stata layout") name(default)
                      
                      graph bar price if rep78>3, over(rep78, sort(1)  ///
                      relabel(2 `""very poor, but not" "as bad as the" "other guys""' ///
                              1 `""somewhat better," "though still far" "from perfect""')) ///
                      title("Desired Stata layout") name(better)
                      
                      graph combine default better , rows(1) xsize(16) ysize(9)

                  Excel rendering:
                  Click image for larger version

Name:	excel_wrap.png
Views:	1
Size:	11.4 KB
ID:	1557163

                  Comment


                  • #10
                    wow, really thanks for all of you!

                    Comment


                    • #11
                      and I have a question that how to change the size of y axis and x axis. (1) I do ylabel(0.2(0.2)0.7) but y axis doesnot start from 0.2,it still start from 0;(2)how to change the size of bar so that It looks thiner.

                      Comment


                      • #12
                        and I have a question that how to change the size of y axis and x axis.
                        There is no x-axis in a bar graph, but a categorical axis.

                        (1) I do ylabel(0.2(0.2)0.7) but y axis doesnot start from 0.2,it still start from 0
                        While Stata will allow you to do this, bar graphs start at 0 and you should not aim to define a different starting point. Also note that you cannot truncate bars but only expand the range of the axis.

                        Code:
                        gr bar...., yscale(range(0.2 0.7)) exclude0
                        (2)how to change the size of bar so that It looks thinner.
                        Depends if you are talking about the aspect ratio or simply the width of the bars. To decrease the width of the bars, specify a larger bar gap

                        Code:
                        gr bar..., over(group, gap(200))
                        To have length > width, specify aspect ratio > 1


                        Code:
                        gr bar..., over(group) aspectratio(1.5)
                        Last edited by Andrew Musau; 06 Jun 2020, 09:03.

                        Comment


                        • #13
                          Wanting to suppress zero on the scale implies that you would be better off with graph dot.

                          Code:
                          graph dot (asis) 系数, over( 行业, sort(1)) exclude0 yla(0.2(0.1)0.6, format(%02.1f)) ysc(alt r(0.15 0.65)) ytitle( 系数)


                          Click image for larger version

Name:	another_dot.png
Views:	1
Size:	28.6 KB
ID:	1557452


                          Comment


                          • #14
                            Originally posted by Nick Cox View Post
                            Wanting to suppress zero on the scale implies that you would be better off with graph dot.

                            Code:
                            graph dot (asis) 系数, over( 行业, sort(1)) exclude0 yla(0.2(0.1)0.6, format(%02.1f)) ysc(alt r(0.15 0.65)) ytitle( 系数)


                            [ATTACH=CONFIG]n1557452[/ATTACH]
                            many thanks

                            Comment


                            • #15
                              @Sergiy Radyakin How about this?

                              Code:
                                  
                                  
                              linewrap , longstring(`"very poor, but not as bad as the other guys"') maxlength(15)
                              foreach i of numlist 1/`r(nlines)' {
                                  local label2 = `"`label2'"' + `""`r(line`i')'""'
                              }
                              
                              linewrap , longstring(`"somewhat better, though still far from perfect"') maxlength(15)
                              foreach i of numlist 1/`r(nlines)' {
                                  local label1 = `"`label1'"' + `""`r(line`i')'""'
                              }
                              
                              graph bar price if rep78>3, over(rep78, sort(1)  ///
                              relabel(2 `"`label2'"'  ///
                                      1 `"`label1'"'  )) ///
                              title("Desired Stata layout") name(better2, replace)
                              Producing
                              Click image for larger version

Name:	better2.png
Views:	1
Size:	53.6 KB
ID:	1594167


                              It looks like the same approach could be used with Chinese characters as follows:

                              Code:
                              linewrap, longstring("系 数") word
                              return list
                              Produces
                              Code:
                              macros:
                                           r(nlines) : "2"
                                            r(line2) : "数"
                                            r(line1) : "系"

                              Available from:

                              Code:
                              view net describe linewrap, from("http://digital.cgdev.org/doc/stata/MO/Misc")
                              Perhaps I should add an option -stack- which would leave the stacked `label1' macro in returned results, obviating the need for the -foreach- loop after -linewrap-..

                              Comment

                              Working...
                              X