Announcement

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

  • Catplot Label Customization

    Hello!

    I have three questions when using catplot.

    1) Labels (designated as the supaxis) were truncated. Is there a way to increase the number of characters displayed or increase the region on the left side of the graph to allow more characters to be displayed?
    Graph:
    Click image for larger version

Name:	truncated before relabel.png
Views:	1
Size:	109.4 KB
ID:	1559789


    Code:
    recode alcgetw (1 = .)
    recode year (2020 = 1) (2018 = 2)

    splitvallabels alcgetw
    catplot year alcgetw, ///
    percent(year) ///
    var1opts(gap(*0) label(labsize(vsmall)) relabel(1 "2020" 2 "2018")) ///
    var2opts(sort(1) descending label(labsize(vsmall))) ///
    ytitle("Percent", size(small)) ///
    title("Most Recent Location of Obtaining Alcohol" ///
    , span size(medium)) ///
    blabel(bar, format(%9.1f)) ///
    intensity(25) ///
    bar(1, color(navy)) ///
    bar(2, color(maroon)) ///

    2) When using relabel to shorten the labels, the space to the left of the graph actually becomes smaller, further limiting the number of characters shown.
    Graph:
    Click image for larger version

Name:	truncated after relabel.png
Views:	1
Size:	94.8 KB
ID:	1559790

    Code:
    recode alcgetw (1 = .)
    recode year (2020 = 1) (2018 = 2)

    splitvallabels alcgetw
    catplot year alcgetw, ///
    percent(year) ///
    var1opts(gap(*0) label(labsize(vsmall)) relabel(1 "2020" 2 "2018")) ///
    var2opts(sort(1) descending label(labsize(vsmall)) relabel(1 "Not" 2 "Home" 6 "Friend's house non-nghbrhd" 5 "Friend's house nghbrhd" 3 "Relative's house" 8 "Store non-nghbohd" 10 "Other place non-nghbrhd" 7 "Store nghbrhd" 9 "Other place nghbrhd" 4 "School" 11 "Other")) ///
    ytitle("Percent", size(small)) ///
    title("Most Recent Location of Obtaining Alcohol" ///
    , span size(medium)) ///
    blabel(bar, format(%9.1f)) ///
    intensity(25) ///
    bar(1, color(navy)) ///
    bar(2, color(maroon)) ///

    3) I wanted the 2020 bars to be one color and the 2018 bars to be another color, but the bar(#, color()) option has not been working. Not sure if there's another way to go about it.

    I'm very much new to State and Statalist, so any input is much appreciated! Thank you!

  • #2
    catplot and splitvallabels are community-contributed commands from SSC, as you are asked to explain Please see https://www.statalist.org/forums/help#stata

    However, everything in the question hinges on what official code
    graph hbar does when called up.

    (1) This is a matter of the underlying Stata code truncating at 32 characters. See this thread and @Scott Merryman's nice analysis. https://www.statalist.org/forums/for...els-on-a-graph

    (2) It is hard to experiment without a data example from you. https://www.statalist.org/forums/help#stata again.

    (3) The options are not working because you are asking to plot one outcome variable. You need the asyvars option and then two colours will appear automatically for your data and you can change them.

    Comment


    • #3
      Thank you for your help and insight! I apologize for being unfamiliar with the help etiquette. I was able to get some help on using dataex.
      1) I think I will leave the underlying code alone for now, as I am uncomfortable messing with it at my skill level.
      2) I see that dataex should be included in State 15.1; I am also using catplot, splitvallabels, and randomtag, which are community-contributed commands from SSC in Stata 15.1
      catplot and splitvallabels were used for the graph. I used randomtag to pull a random sample of 100 from my data; then, I used dataex on that sample.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte alcgetw float year
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       2 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       1 2018
       2 2018
      10 2018
       1 2018
       1 2018
       6 2018
       1 2018
       2 2018
       1 2018
       2 2018
       1 2018
       . 2018
       1 2018
       1 2018
       5 2018
      11 2018
       1 2018
      11 2018
       . 2018
       8 2018
       8 2018
       2 2018
       3 2018
       1 2018
       8 2018
       1 2018
       1 2018
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       . 2020
       6 2020
       1 2020
       6 2020
       1 2020
       1 2020
       2 2020
       1 2020
       1 2020
       1 2020
       1 2020
       1 2020
       2 2020
       2 2020
       6 2020
       1 2020
       1 2020
       1 2020
       1 2020
      11 2020
       1 2020
      11 2020
       2 2020
       1 2020
       1 2020
       1 2020
      11 2020
       1 2020
       5 2020
       1 2020
       1 2020
       6 2020
       6 2020
       6 2020
       6 2020
       1 2020
      end
      label values alcgetw lagu8
      label def lagu8 1 "NA / never drank alcohol", modify
      label def lagu8 2 "Home", modify
      label def lagu8 3 "A relative's house", modify
      label def lagu8 5 "Friend's house in my neighborhood", modify
      label def lagu8 6 "Friend's house outside my neighborhood", modify
      label def lagu8 8 "Store outside my neighborhood", modify
      label def lagu8 10 "Some other place outside the neighborhood", modify
      label def lagu8 11 "Other", modify
      3) Thank you! I added asyvars back in and it did work with the bar(#, color()). I had previously tried the asyvars option and removed it after it had no effect.

      Code:
      recode alcgetw (1 = .)
      recode year (2020 = 1) (2018 = 2)
      
      splitvallabels alcgetw
      catplot year alcgetw, ///
      percent(year) ///
      var1opts(gap(*0) label(labsize(vsmall)) relabel(1 "2020" 2 "2018")) ///
      var2opts(sort(1) descending label(labsize(vsmall))) ///
      ytitle("Percent", size(small)) ///
      title("Most Recent Location of Obtaining Alcohol" ///
      , span size(medium)) ///
      blabel(bar, format(%9.1f)) ///
      intensity(25) ///
      asyvars ///
      bar(1, color(navy)) ///
      bar(2, color(maroon)) ///
      Click image for larger version

Name:	asyvars before relabel.png
Views:	1
Size:	92.1 KB
ID:	1560275


      Code:
      recode alcgetw (1 = .)
      recode year (2020 = 1) (2018 = 2)
      
      splitvallabels alcgetw
      catplot year alcgetw, ///
      percent(year) ///
      var1opts(gap(*0) label(labsize(vsmall)) relabel(1 "2020" 2 "2018")) ///
      var2opts(sort(1) descending label(labsize(vsmall)) relabel(1 "Not" 2 "Home" 6 "Friend's house non-nghbrhd" 5 "Friend's house nghbrhd" 3 "Relative's house" 8 "Store non-nghbohd" 10 "Other place non-nghbrhd" 7 "Store nghbrhd" 9 "Other place nghbrhd" 4 "School" 11 "Other")) ///
      ytitle("Percent", size(small)) ///
      title("Most Recent Location of Obtaining Alcohol" ///
      , span size(medium)) ///
      blabel(bar, format(%9.1f)) ///
      intensity(25) ///
      asyvars ///
      bar(1, color(navy)) ///
      bar(2, color(maroon)) ///
      Click image for larger version

Name:	asyvars after relabel.png
Views:	1
Size:	67.2 KB
ID:	1560276

      Comment


      • #4
        After changing around the labels, I think the problem with the left-side spacing could be a glitch/bug caused by apostrophes in the relabel text.
        Community-contributed commands explained and dataex provided in previous reply.

        WITH apostrophes in the relabel text:
        Code:
        recode alcgetw (1 = .)
        recode year (2020 = 1) (2018 = 2)
        
        splitvallabels alcgetw
        catplot year alcgetw, ///
        percent(year) ///
        var1opts(gap(*0) label(labsize(vsmall)) relabel(1 "2020" 2 "2018")) ///
        var2opts(sort(1) descending label(labsize(vsmall)) relabel(2 "Home" 6 "Friend's house non-nghbrhd" 5 "Friend's house nghbrhd" 3 "Relative's house" 8 "Store non-nghbohd" 10 "Other place non-nghbrhd" 7 "Store nghbrhd" 9 "Other place nghbrhd" 4 "School" 11 "Other")) ///
        ytitle("Percent", size(small)) ///
        title("Most Recent Location of Obtaining Alcohol" ///
        , span size(medium)) ///
        blabel(bar, format(%9.1f)) ///
        intensity(25) ///
        asyvars ///
        bar(1, color(navy)) ///
        bar(2, color(maroon)) ///
        Click image for larger version

Name:	relabel with apostrophe.png
Views:	1
Size:	71.4 KB
ID:	1560440



        WITHOUT apostrophes in the relabel text:
        Code:
        recode alcgetw (1 = .)
        recode year (2020 = 1) (2018 = 2)
        
        splitvallabels alcgetw
        catplot year alcgetw, ///
        percent(year) ///
        var1opts(gap(*0) label(labsize(vsmall)) relabel(1 "2020" 2 "2018")) ///
        var2opts(sort(1) descending label(labsize(vsmall)) relabel(2 "Home" 6 "Friends house non-nghbrhd" 5 "Friends house nghbrhd" 3 "Relatives house" 8 "Store non-nghbohd" 10 "Other place non-nghbrhd" 7 "Store nghbrhd" 9 "Other place nghbrhd" 4 "School" 11 "Other")) ///
        ytitle("Percent", size(small)) ///
        title("Most Recent Location of Obtaining Alcohol" ///
        , span size(medium)) ///
        blabel(bar, format(%9.1f)) ///
        intensity(25) ///
        asyvars ///
        bar(1, color(navy)) ///
        bar(2, color(maroon)) ///
        Click image for larger version

Name:	relabel without apostrophe.png
Views:	1
Size:	90.5 KB
ID:	1560441

        Comment

        Working...
        X