Announcement

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

  • Overlapping legend labels using catplot

    I am using catplot from SSC in Stata 15.1.

    I have survey data. Respondents attended a workshop and indicated how relevant the session was to them and listed their years of work experience:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(relevance experience)
    5 4
    5 1
    5 1
    5 6
    4 2
    4 5
    4 4
    5 1
    5 2
    4 4
    4 5
    5 1
    5 1
    5 5
    4 5
    4 1
    5 4
    5 1
    4 2
    5 4
    5 6
    5 1
    5 5
    5 3
    5 6
    5 1
    4 6
    5 3
    5 4
    5 1
    4 1
    5 1
    5 6
    4 2
    5 1
    5 1
    5 2
    4 5
    5 2
    5 2
    5 1
    5 6
    4 3
    4 5
    3 1
    4 5
    4 1
    1 6
    5 5
    5 1
    5 1
    5 1
    5 1
    5 1
    5 1
    5 2
    4 5
    5 2
    4 1
    4 1
    5 1
    5 1
    5 5
    5 1
    5 2
    5 3
    5 3
    4 6
    5 1
    5 1
    5 4
    5 5
    5 5
    4 4
    5 2
    5 3
    5 2
    4 3
    5 1
    5 1
    4 5
    5 3
    5 4
    5 3
    4 4
    4 2
    4 6
    5 2
    3 5
    5 2
    5 4
    5 2
    4 1
    5 3
    4 4
    5 3
    1 2
    5 1
    5 1
    5 1
    end
    label values relevance relevance
    label def relevance 1 "Unsure", modify
    label def relevance 3 "Not really relevant", modify
    label def relevance 4 "Somewhat relevant", modify
    label def relevance 5 "Very relevant", modify
    label values experience experience
    label def experience 1 "25+", modify
    label def experience 2 "21-25", modify
    label def experience 3 "16-20", modify
    label def experience 4 "11-15", modify
    label def experience 5 "6-10", modify
    label def experience 6 "<5", modify
    I am using a stacked bar chart to graph how relevant the session was to attendees by years of work experience. My code is:

    Code:
    catplot relevance experience, percent(experience) asyvars stack title("How Relevant was the Session?") ///
    subtitle(by years of work experience) blabel(bar, pos(center) format(%2.0f)) legend(pos(bottom) col(5))
    This code gives me the graph I want, but as seen below, the legend labels overlap:
    Click image for larger version

Name:	relevance_exp.png
Views:	1
Size:	78.1 KB
ID:	1526674



    This problem curiously did not arise until I used the blabel option to include percentages on the graph. Thanks for reading.

  • #2
    use the -size() and/or the -symxsize()- options within legend:

    Code:
    legend(pos(bottom) col(5) size(*.75)  symxsize(*.75))

    Comment


    • #3
      As a friendly reminder, FAQ 12.1 asks posters to state the author and source of user-written programs. catplot is written by Nick Cox and is available from SSC and was introduced in Cox, N. J. (2004). Speaking Stata: Graphing categorical and compositional data. The Stata Journal, 4(2), 190-215.

      (I have made this mistake myself, so I understand how it might happen.)
      David Radwin
      Senior Researcher, California Competes
      californiacompetes.org
      Pronouns: He/Him

      Comment


      • #4
        Thank you for your responses. I resolved the issue by simply omitting the legend option.

        Code:
        catplot relevance experience, percent(experience) asyvars stack title("How Relevant was the Session?") ///
        subtitle(by years of work experience) blabel(bar, pos(center) format(%2.0f))

        Comment


        • #5
          Although a solution was reported, I suggest a different solution, avoiding catplot altogether.

          1. A legend is at best a necessary evil, requiring back and forth from the reader. I prefer information to be given more directly using axis labels or text on the graph.

          2. Stacked bars are a familiar design, but I have to come to see them as oversold. It's hard to see categories that have small frequencies (including zeros!) and hard to combine bars and explicit numbers readably. Only the first and last categories can be compared easily. The visual reminder when stacking percents that they add to 100% is perhaps reassuring but hardly necessary except for naive readers.

          An alternative design for your data -- and to be sure, it has some limitations too -- is a two-way bar chart. I used tabplot and in particular the latest version of the code which as I write is on SSC only. https://www.stata-journal.com/articl...article=gr0066 is the fullest write-up while https://www.statalist.org/forums/for...updated-on-ssc gives a brisker summary and in particular discusses the frame option used here.

          For the convenience of anyone interested this code includes Greg's data from #1, his solution in #4 and a new solution. However, I fixed the value labels. The existing labels imply that 5 years of experience is impossible and leave ambiguous what happens with reports of 25 years.

          Naturally there is indefinite scope to tinker. You can have different colours for different categories, just as in grade school, and so forth.

          Code:
          clear
          input long(relevance experience)
          5 4
          5 1
          5 1
          5 6
          4 2
          4 5
          4 4
          5 1
          5 2
          4 4
          4 5
          5 1
          5 1
          5 5
          4 5
          4 1
          5 4
          5 1
          4 2
          5 4
          5 6
          5 1
          5 5
          5 3
          5 6
          5 1
          4 6
          5 3
          5 4
          5 1
          4 1
          5 1
          5 6
          4 2
          5 1
          5 1
          5 2
          4 5
          5 2
          5 2
          5 1
          5 6
          4 3
          4 5
          3 1
          4 5
          4 1
          1 6
          5 5
          5 1
          5 1
          5 1
          5 1
          5 1
          5 1
          5 2
          4 5
          5 2
          4 1
          4 1
          5 1
          5 1
          5 5
          5 1
          5 2
          5 3
          5 3
          4 6
          5 1
          5 1
          5 4
          5 5
          5 5
          4 4
          5 2
          5 3
          5 2
          4 3
          5 1
          5 1
          4 5
          5 3
          5 4
          5 3
          4 4
          4 2
          4 6
          5 2
          3 5
          5 2
          5 4
          5 2
          4 1
          5 3
          4 4
          5 3
          1 2
          5 1
          5 1
          5 1
          end
          label values relevance relevance
          label def relevance 1 "Unsure", modify
          label def relevance 3 "Not really relevant", modify
          label def relevance 4 "Somewhat relevant", modify
          label def relevance 5 "Very relevant", modify
          label values experience experience
          label def experience 1 ">25", modify
          label def experience 2 "21-25", modify
          label def experience 3 "16-20", modify
          label def experience 4 "11-15", modify
          label def experience 5 "6-10", modify
          label def experience 6 "<6", modify
          
          set scheme s1color 
          
          catplot relevance experience, percent(experience) asyvars stack title("How Relevant was the Session?") ///
          subtitle(by years of work experience) blabel(bar, pos(center) format(%2.0f)) name(gs_catplot) 
          
          tabplot relevance experience, percent(experience) title("How Relevant was the Session?") ///
          showval(format(%2.0f)) xreverse yreverse subtitle(%) ytitle("") xtitle(Years of experience) frame(100) ///
          name(gs_tabplot)
          Click image for larger version

Name:	saidutte.png
Views:	1
Size:	29.0 KB
ID:	1526737

          Comment

          Working...
          X