Announcement

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

  • Bug in graph bar(?)

    Hello everyone,
    I am trying, for an hour, to do a very simple bar graph in principle, but stata somehow makes it very complicated.

    For some reason, my x labels, which is the variable through which I group the bars, go off-screen. Usually, stata shrinks the graph in order to insert all the text of the label but in this case, it fails miserably and I do not understand why.

    So my code is:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    
    clear
    input str32(groupedvar thisoneworks)
    "Studi universitari"               "Scuola media superiore"
    "Iniziare un'attività lavorativa" "Scuola media superiore"
    "Altro-Non so"                     "Master e/o Dottorato"  
    "Studi universitari"               "Scuola media superiore"
    "Spese personali"                  "Scuola media superiore"
    "Iniziare un'attività lavorativa" "Laurea"                
    "Iniziare un'attività lavorativa" "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Contribuire a spese familiari"    "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Iniziare un'attività lavorativa" "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Risparmiare"                      "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Corsi professionalizzanti"        "Scuola media superiore"
    "Corsi professionalizzanti"        "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Corsi professionalizzanti"        "Scuola media superiore"
    "Corsi professionalizzanti"        "Scuola media superiore"
    "Studi universitari"               "Scuola media superiore"
    "Iniziare un'attività lavorativa" "Scuola media superiore"
    "Corsi professionalizzanti"        "Scuola media superiore"
    "Contribuire a spese familiari"    "Scuola media superiore"
    "Altro-Non so"                     "Scuola media superiore"
    "Studi universitari"               "Master e/o Dottorato"  
    end
    
    
    graph bar (count), over(thisoneworks, label(angle(45)))
    graph bar (count), over(groupedvar, label(angle(45)))

    As you can see with the "thisoneworks" variable everything is fine, while with the "groupedvar" no. Does anyone know why this is happening and how can be fixed?

    Thank you in advance!
    Last edited by Demetrio Guzzardi; 12 May 2020, 04:20.

  • #2
    The underlying issue is that graph is struggling here given the long text strings to serve as axis labels. Various solutions, which can be combined, are

    1. Using graph hbar instead.

    2. Abbreviating the labels. (I don't know enough Italian, or rather project from my other languages well enough, to make good suggestions.)

    3. Splitting longer labels into two lines.

    4. Changing the aspect ratio.

    I did this with your data example (thanks!). My own bias is that
    angle(45) is only rarely a good solution and very rarely the best solution. The order of bars can surely be improved.


    Code:
    set scheme s1color 
    replace groupedvar = `""Iniziare un'attività" "avorativa" ""' if groupedvar == "Iniziare un'attività lavorativa"
    replace groupedvar = `""Contribuire a spese" "familiari" "' if groupedvar == "Contribuire a spese familiari"
    replace groupedvar = `""Corsi" "professionalizzanti""' if groupedvar == "Corsi professionalizzanti"  
    graph hbar (count), over(thisoneworks) name(G1, replace)
    graph hbar (count), over(groupedvar) name(G2, replace) aspect(1.2)


    Click image for larger version

Name:	guzzardi.png
Views:	1
Size:	23.2 KB
ID:	1552658


    Comment


    • #3
      Thank you Nick, I like your work-around and will try to break the lines.
      However on a more technical level, I am not sure that the problem is about the lenght of the string.
      I tried with longer strings and it still works as with the shorter ones. The graph get collapsed and the text is shown "fully". Here is an example:

      Code:
      clear
      input str82 thisworkstoo
      "studi universitari"                                                                
      "una base da utilizzare o subito o in futuro per iniziare un’attività lavorativa"
      "non so"                                                                            
      "studi universitari"                                                                
      "spese personali"                                                                  
      "una base da utilizzare o subito o in futuro per iniziare un’attività lavorativa"
      "una base da utilizzare o subito o in futuro per iniziare un’attività lavorativa"
      "studi universitari"                                                                
      "contribuire alle spese della tua famiglia"                                        
      "studi universitari"                                                                
      "una base da utilizzare o subito o in futuro per iniziare un’attività lavorativa"
      "studi universitari"                                                                
      "studi universitari"                                                                
      "risparmiare"                                                                      
      "studi universitari"                                                                
      "studi universitari"                                                                
      "studi universitari"                                                                
      "studi universitari"                                                                
      "corsi professionalizzanti"                                                        
      "corsi professionalizzanti"                                                        
      "studi universitari"                                                                
      "studi universitari"                                                                
      "corsi professionalizzanti"                                                        
      "corsi professionalizzanti"                                                        
      "studi universitari"                                                                
      "una base da utilizzare o subito o in futuro per iniziare un’attività lavorativa"
      "corsi professionalizzanti"                                                        
      "contribuire alle spese della tua famiglia"                                        
      "finalità diverse da quelle qui indicate"                                          
      "studi universitari"                                                                
      end
      
      graph bar (count), over(thisworkstoo , label(angle(45)) )
      Click image for larger version

Name:	image_18140.png
Views:	1
Size:	72.0 KB
ID:	1552671

      Comment


      • #4
        deleted
        Last edited by Andrew Musau; 12 May 2020, 07:40.

        Comment


        • #5
          I don't know why #3 is considered a bug in the software. The request being made to show long text strings can't be accommodated reasonably. The command won't change text size unilaterally, so the bars have to give.

          Comment


          • #6
            Please ignore #4, re-reading #1, I thought the issue was that the labels are truncated. Nick is right that something has to give.

            Comment


            • #7
              Sorry with the deletion of one of the post I think I missed something... So you are saying the problem is that in the first case the importance of the height of the bars is more important than the lengths of the text, while in the other cases the length of the text is privileged?

              Comment


              • #8
                Andrew Musau made a comment and then changed his mind. I won't fill in what he wanted to delete, but it was on a technical point, only.

                I am not saying either. If I can't fit into my clothes, you can take that as the clothes being the wrong size or my needing to lose weight. The question of blame is not the key issue, except for me: it is just a fact if there is a lack of fit.

                Similarly, with graphs.

                Sometimes graph truncates text labels and other stuff associated with axes, and sometimes it squeezes the plot region. I didn't write any of the source code, nor have I read it, nor can I read it, and for all of these reasons I can't comment on why graph jumps one way or the other in a squeeze. It's just hard experience that you can't fit an enormous amount on a graph -- unless exceptionally, and this is important when it is, you are printing out at poster size or displaying on a monitor the size of a mammoth.

                It's not my job either to defend StataCorp's code if it doesn't work well. It isn't doing what you want here, and we are discussing why.

                But on a user level, and on a user-programmer level, I've wrestled with designing enough graphs, and with designing enough graph commands on top of Stata's graphics, to have a lot of sympathy and sensitivity to the difficult trade-offs in graphics.
                Last edited by Nick Cox; 12 May 2020, 09:16.

                Comment


                • #9
                  Sorry, I should have kept the post as there are two issues at play here. Nick's point holds that if you force the full labels to be displayed, the bars will shrink. Your graph #2 (which you should have posted) illustrates the problems reported in the following links and as you note, it doesn't appear to be the length of text that triggers this behavior (see the first link, for example).

                  https://www.statalist.org/forums/for...dot-graph-hbar
                  https://www.statalist.org/forums/for...ls-for-y-and-x

                  Your graph #2
                  Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.6 KB
ID:	1552714



                  The workaround is to explicitly specify how much space you want allocated to the outer margin.

                  Code:
                  graph bar (count), over(groupedvar, axis(outergap(30))  label(angle(45)))
                  Click image for larger version

Name:	Graph.png
Views:	1
Size:	44.3 KB
ID:	1552715




                  This appears to be a Stata 16 issue and I agree with you that it looks like a bug. You should draw the attention of this thread to Technical Services so that they can determine whether this is the case.
                  Last edited by Andrew Musau; 12 May 2020, 09:12.

                  Comment


                  • #10
                    Alright! Thank you both very much for your help and your suggestions! Thank you Nick for the workaround and thank you Andrew for the "axis(outergap(30))" option which I was not aware of.

                    Comment


                    • #11
                      Sometimes you get a ridiculous-looking graph and I think of this as the programmer reaching through the code to talk, and saying "You need to think more about what you are asking". A canonical example is asking for logarithmic scale while insisting that zero be shown on the corresponding axis.

                      Comment

                      Working...
                      X