Announcement

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

  • Increasing inner graph region to allow longer categorical x axis labels without overlapping – catplot with splitvallabels

    Dear Statalist,
    I am plotting a horizontal bar chart using catplot. Each categorical x axis label is quite long so I am trying to include them on several lines using splitvallabels. But due to their length, the labels overlap or must be tiny because I cannot increase the width of the 'text box' they are written within. I believe the solution might be to increase the size of the inner graph region.

    I have tried to do this by modifying the aspect ratio using margins and other y/x axis ratio options but am failing (some examples below).

    I wonder if anyone can advise how to increase the width of the labels so they can be included in 2 or 3 rows?

    Thank you very much for any guidance.

    Josh.

    Graph_2.pngGraph_1.png

  • #2
    Although you used catplot (from SSC, as you are asked to explain) and splitvallabels (ditto) this doesn't raise any question about either, but more about graph hbar, which is the underlying command.

    You would gain a little real estate by asking for your legend to be on one row. Changing ysize() often helps directly.

    I'd shorten the questions myself.

    No code, no data example.

    Comment


    • #3
      Thank you Nick for the reply and clarifying this is a graph hbar question; and sorry for the omissions. Please find a data example and corresponding code below.

      Shortening the questions is an option but I would then need to link to the precise language elsewhere. It would be preferable to dedicate more of the plot area to the axis labels so all of the information is in one place.

      Thanks for the propositions with legend and ysize() which help a little (some example below).

      I am really looking though for a way to increase the size of what I believe is in 'inner graph region' (documents here: https://www.stata.com/manuals/g-3region_options.pdf) to control the shape of the text boxes the labels are written in.

      Any further suggestions gratefully received.

      Josh.

      Code:
      splitvallabels question
      catplot q question, ///
          var1opts(label(labsize(small))) ///
          var2opts(label(labsize(small)) relabel(`r(relabel)') ) ///
          legend (row(1)) ///
          asyvars stack ///
          ysize (6)

      Graph_4.pngGraph_5.png


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(q question)
      2 1
      1 2
      2 3
      2 4
      2 5
      2 1
      1 2
      2 3
      2 4
      2 5
      2 1
      2 2
      2 3
      2 4
      2 5
      2 1
      2 2
      2 3
      2 4
      1 5
      2 1
      2 2
      2 3
      2 4
      1 5
      2 1
      1 2
      2 3
      2 4
      2 5
      2 1
      2 2
      2 3
      2 4
      1 5
      2 1
      2 2
      2 3
      2 4
      1 5
      2 1
      1 2
      3 3
      3 4
      2 5
      2 1
      2 2
      2 3
      2 4
      1 5
      2 1
      2 2
      2 3
      3 4
      1 5
      2 1
      2 2
      1 3
      3 4
      1 5
      2 1
      2 2
      1 3
      1 4
      1 5
      2 1
      2 2
      2 3
      2 4
      1 5
      2 1
      2 2
      2 3
      2 4
      1 5
      end
      label values q Doesthestudyassessaclearfo
      label def Doesthestudyassessaclearfo 1 "No", modify
      label def Doesthestudyassessaclearfo 2 "Yes", modify
      label def Doesthestudyassessaclearfo 3 "unsure", modify
      label values question study_q
      label def study_q 1 "Does the study assess a clear and focused research question?", modify
      label def study_q 2 "Was haemophilia and blood loss the primary research question of interest?", modify
      label def study_q 3 "Were methods clearly defined and appropriate?", modify
      label def study_q 4 "Were the populations recruited/selected in an appropriate and representative way?", modify
      label def study_q 5 "Was the exposure accurately measured to minimise bias?", modify

      Comment


      • #4
        I am continuing, perversely or otherwise, to answer a different but perhaps related question, which is "What would I do with these data?".

        Take 2:

        As often in this forum, I suggest that the stacked bar design -- although easy to understand in principle -- is not especially effective in practice. The message that the total is the same across rows is surely not needed here. The stacked bar design is not good at making obvious when a bar is absent or at showing small quantities when they exist. It does not make vertical comparisons easy.

        As these data could easily be tabulated, it's a clear goal that a graph should be at least as clear as the corresponding table, and ideally clearer.

        The graph scheme you've chosen -- or let Stata choose for you, as it is the default s2color -- yields here rather miscellaneous colours, which to me are not obviously ordered. By filling much of the space with fairly strong colours it produces a graph that is heavy on the eye. I've shifted my views on colour since reading Claus WIlke's excellent book (see my review at https://www.amazon.com/gp/customer-r...R22MWD7RJ6QAFP)

        Mixing green and red is always dangerous. I find that even biologists and medics do it, who should know better than most people about so-called colour blindness.

        Surely [sic] "unsure" belongs between "Yes" and "No" and should be "Unsure" too for consistency.

        Otherwise I shortened the questions and used tabplot from the Stata Journal.


        Code:
        . search tabplot, sj
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        SJ-20-3 gr0066_2  . . . . . . . . . . . . . . . .  Software update for tabplot
                (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/20   SJ 20(3):757--758
                added new options frame() and frameopts() allowing framing
                of bars and so-called thermometer plots or charts
        
        SJ-17-3 gr0066_1  . . . . . . . . . . . . . . . .  Software update for tabplot
                (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/17   SJ 17(3):779
                added options for reversing axis scales; improved handling of
                axis labels containing quotation marks
        
        SJ-16-2 gr0066  . . . . . .  Speaking Stata: Multiple bar charts in table form
                (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
                Q2/16   SJ 16(2):491--510
                provides multiple bar charts in table form representing
                contingency tables for one, two, or three categorical variables
        The main write-up is accessible at https://www.stata-journal.com/articl...article=gr0066 but download the most recent version of the software files if interested.

        Alternatively, the thread https://www.statalist.org/forums/for...updated-on-ssc gives an overview.

        Here's my attempt and the complete code.



        Small patches of strong colour seem more defensible than large patches of fairly strong colour, but like so much that can be tuned.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(q question)
        2 1
        1 2
        2 3
        2 4
        2 5
        2 1
        1 2
        2 3
        2 4
        2 5
        2 1
        2 2
        2 3
        2 4
        2 5
        2 1
        2 2
        2 3
        2 4
        1 5
        2 1
        2 2
        2 3
        2 4
        1 5
        2 1
        1 2
        2 3
        2 4
        2 5
        2 1
        2 2
        2 3
        2 4
        1 5
        2 1
        2 2
        2 3
        2 4
        1 5
        2 1
        1 2
        3 3
        3 4
        2 5
        2 1
        2 2
        2 3
        2 4
        1 5
        2 1
        2 2
        2 3
        3 4
        1 5
        2 1
        2 2
        1 3
        3 4
        1 5
        2 1
        2 2
        1 3
        1 4
        1 5
        2 1
        2 2
        2 3
        2 4
        1 5
        2 1
        2 2
        2 3
        2 4
        1 5
        end
        label values q Doesthestudyassessaclearfo
        replace q = 5 - q if q >= 2
        label def Doesthestudyassessaclearfo 1 "No", modify
        label def Doesthestudyassessaclearfo 3 "Yes", modify
        label def Doesthestudyassessaclearfo 2 "Unsure", modify
        label values question study_q
        label def study_q 1 `" "clear and focused" "research question?" "', modify
        label def study_q 2 `" "haemophilia and blood loss" "primary research question?" "', modify
        label def study_q 3 `" "methods clearly defined" "and appropriate?" "', modify
        label def study_q 4 `" "populations recruited/selected" "appropriately/representatively?" "', modify
        label def study_q 5 `" "exposure accurately measured" "to minimise bias?" "', modify
        
        tabplot question q, showval ytitle("") xtitle("") yla(, labsize(small)) xla(, labsize(small)) subtitle(, size(small)) xreverse horizontal separate(q) bar2(bfcolor(blue*0.1) blcolor(blue)) bar1(color(red))

        Comment


        • #5
          Hi Nick, thank you very much for the detailed response, both the practical bits and also the more theoretical. I was planning on sending this to an artist friend for a view on colours, which I always struggle with, but will also have a look at the book you recommended. Your proposed colours are already a big improvement.

          I have also read your previous comments on stacked bars - thank you for the idea using tabplot. I will experiment with the full data set.

          Josh.

          Comment

          Working...
          X