Announcement

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

  • Suppress legend on histogram

    Hello all,
    I would like to create a histogram of a 4-level categorical variable (hh_comp) that describes household composition, by level of food security (4-level categorical variable, hh_fsec4).
    Code:
    hist hh_fsec4 if timepoint ==0, by(hh_comp, graphregion(color(white))) percent addlabels discrete xlabel (1 "High" 2 "Marginal" 3 "Low" 4 "Very low")

    This works well, except that the label at the bottom is not helpful and just takes up space. I realize I can fix this in graph editor. However, I will create this graph across multiple timepoints using a loop, and would therefore prefer to automate the process. I tried

    Code:
    hist hh_fsec4 if timepoint ==0, by(hh_comp, graphregion(color(white))) percent addlabels discrete xlabel (1 "High" 2 "Marginal" 3 "Low" 4 "Very low") legend(order 1 2 3 4)
    because of another post on Statalist. However, that was for a scatterplot I believe, and I learned that doesn't work with histograms because Stata informed me that "option order not allowed."

    Thank you for any advice!

  • #2
    Can you show us the graph?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Perhaps you are alluding to the note. See if this does what you want:


      Code:
        
       by(hh_comp, graphregion(color(white)) note(""))
      Code:
      legend(order(1 2 3 4))
      is legal but I think irrelevant here.

      Comment


      • #4
        Thank you to you both! Nick, you're right; adding in the pair of parentheses allowed the code to run:

        Code:
        hist hh_fsec4 if timepoint ==0, by(hh_comp, graphregion(color(white))) percent addlabels discrete xlabel (1 "High" 2 "Marginal" 3 "Low" 4 "Very low") legend(order(1 2 3 4))
        However, it did not have the effect I was hoping for. Maarten, I've attached a picture of the graph. I appreciate any advice!
        Attached Files

        Comment


        • #5
          You don't need a legend at all. Your y axis already explains what you are showing.
          Code:
          legend(off)
          A broadly equivalent graph here is a two-way bar chart as obtainable with say tabplot (Stata Journal). Here is a gesture towards your data:

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(percent y) byte x
             61 1 1
             11 1 2
             13 1 3
             15 1 4
          69.08 2 1
          8.434 2 2
          10.84 2 3
          11.65 2 4
          54.84 3 1
          11.29 3 2
          17.74 3 3
          16.13 3 4
           58.7 4 1
           12.9 4 2
          14.19 4 3
          14.19 4 4
          end
          label values x x
          label def x 1 "High", modify
          label def x 2 "Marginal", modify
          label def x 3 "Low", modify
          label def x 4 "Very low", modify
          
          
          
          tabplot y x [iw=percent] , showval scheme(s1color) xtitle(Level of food security) ytitle(Living arrangements) bfcolor(none) xsc(titlegap(*5))


          Click image for larger version

Name:	tabplot.png
Views:	1
Size:	22.9 KB
ID:	1599394


          Starting with your data a command would be more like

          Code:
           
           tabplot hh_comp hh_fsec4 if timepoint ==0, percent(hh_comp) showval xlabel (1 "High" 2 "Marginal" 3 "Low" 4 "Very low") 


          .There are many details you could change. Why this rather than yours? There is no issue but taste, but I suggest this design has less scaffolding and graph stuff. For example, if you show the percents you don't need axis labels (although that could be fixed in
          hist too).


          On
          tabplot here is an otherwise unpredictable search term: gr0066


          The 2016 paper is freely accessible and is the fullest account for the foreseeable future, but use the latest update to install the files if interested, so the latest release was in 2020Q3 as I wrote, (The help file on my machine is already longer, although not importantly.)

          Otherwise skim https://www.statalist.org/forums/for...updated-on-ssc for an overview.


          . search gr0066, entry

          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

          (end of search)


          Last edited by Nick Cox; 23 Mar 2021, 08:22.

          Comment

          Working...
          X