Announcement

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

  • Modifying x-axis labels in graph box

    Hi - I was using this following code to generate a box graph.


    Code:
    sysuse auto, clear
    
    forval y = 31/51 {
           if mod(`y', 5) {
                      label def turnz `y' "", add
                 }
           }
    
    label value turn turnz
    
    tab turn
    
    graph box price, over(turn)

    While the value labels were shown in the tab code, they were not reflected in the graph box code.

    Is there a way to have graph box include the label definitions on the category axis label?

    Thanks,

    Peter.
    Last edited by Peter Baade; 22 Feb 2021, 22:36.

  • #2
    Progress. I realised that creating blank labels is recognised with the tab command, but not with graph box.

    If I replaced this code

    Code:
     
      label def turnz `y' "", add
    with
    Code:
     label def turnz `y' ".", add
    then the value labels are incorporated into graph box for the category labels. Is there a way to remove the dot/period from the label in the graph?

    Comment


    • #3
      I played a bit with this without finding a better solution of the kind you ask for. graph box has a built-in bias to regard the distinct categories specified by over() as values you all want to see and giving spaces to value labels isn't a work-around.

      I get this with
      stripplot from SSC. It has other handles you can turn.

      Code:
      sysuse auto, clear
      set scheme s1color 
      stripplot price, over(turn) xla(30(5)50) box(barw(0.8))  vertical



      Click image for larger version

Name:	price_turn.png
Views:	1
Size:	34.4 KB
ID:	1594614

      Comment


      • #4
        My initial thoughts were that there must be a Unicode character that can do the trick. True to form, the braille pattern blank is one such character.

        Code:
        sysuse auto, clear
        
        forval y = 31/51 {
               if mod(`y', 5) {
                          label def turnz `y' "`:di "`=uchar(28)'"'", add
                     }
               }
        
        label value turn turnz
        tab turn
        graph box price, over(turn)
        Res.:

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	54.6 KB
ID:	1594625

        Comment


        • #5
          Thanks for your response, Nick. I found a similar post of yours from 2015 that provided a solution

          https://www.statalist.org/forums/for...-on-the-x-axis

          So the revised code would be
          Code:
          sysuse auto, clear
          
          forval y = 31/51 {
              if mod(`y', 3) {
                  label def turnz `y' `"{c 0xa0}"', add 
              }
          }
          
          label value turn turnz
          
          tab turn
          
          graph box price, over(turn)
          It still treats the values on the x axis as categorical rather than continuous, but this would be modified through data manipulation.

          Comment


          • #6
            Many thanks, Andrew.

            Comment


            • #7
              Andrew Musau That's clever. I tried uchar(160) without success.

              But here's the next challenge for anyone persisting with graph box here. Any reader could be forgiven for imagining that the two rightmost boxes refer to turn values 47 and 48 -- but they don't. They refer to 48 and 51.

              graph box uses a categorical horizontal axis, not a numeric horizontal axis, so numeric categories are ordered, but not shown literally.

              Presumably Peter Baade doesn't care one bit about the auto data. He's just giving us an example we can all use, which is exactly right. What works well with his real data remains an open question, however.

              Comment


              • #8
                Thanks Nick Cox and Andrew Musau, your advice is very helpful. Yes, you are right, Nick - I have no interest in the auto data, this was provided for an example only.

                I have provided some code below that does address the issue of the categorical versus numerical horizontal axis. It adds extra records for every possible value of the variable, and sets those records to have a frequency weight of zero.

                I have also tried to generalise the code, but haven't tested this across other datasets. There are probably less clunky ways of coding this.

                Peter.


                Code:
                sysuse auto, clear
                
                // setting all records to have a weight = 1 //
                gen freqwght=1
                
                // showing the frequency value of the variable turn //
                tab turn
                
                // getting the number of records and the range (maximim and minimum value) of the variable turn //
                summ turn, meanonly
                local max=r(max)
                local min=r(min)
                local N=r(N)
                
                // Making all labels blank, except for every third value
                forval y = `min'/`max' {
                    if mod(`y', 3) {
                        label def turnz `y' "`:di "`=uchar(28)'"'", add
                    }
                }
                
                label value turn turnz
                
                // Adding extra records to the dataset - equivalent to the range of the variable turn //
                local obsnum = `N' + `max' - `min' +1
                set obs `obsnum'
                
                // For those extra records added, set the weight variable equal to zero
                replace freqwght=0 if turn==.
                
                // For those extra records added, set the value of turn to be the individual possible values within the range
                replace turn=_n-`N' + `min' -1 if turn==.
                
                // Generate the box graph using the frequency weight and allcategories options
                graph box price [fw=freqwght], over(turn) allcategories
                
                graph export plot.png, replace
                Click image for larger version

Name:	plot.png
Views:	1
Size:	60.0 KB
ID:	1594761

                Comment

                Working...
                X