Announcement

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

  • Catplot label customization - var1opts

    Hello!

    I am using SSC's catplot to create a simple descriptive graph of the age of my sample (with age as a categorical variable). I want to specify the age labels on the x-axis, however, when I use var1opts (as I have seen in the "help catplot" guide), I get no apparent changes in my graph.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float age_cat byte submission
    . 0
    . 0
    . 0
    . 0
    2 1
    2 1
    . 0
    3 1
    2 1
    4 1
    3 1
    3 1
    4 1
    5 1
    4 1
    4 1
    3 1
    4 1
    . 0
    4 1
    5 1
    . 0
    2 1
    . 0
    . 0
    . 0
    3 1
    2 1
    3 1
    3 1
    3 1
    2 1
    . 0
    . 0
    4 1
    . 0
    4 1
    2 0
    . 1
    3 0
    4 1
    4 1
    2 1
    3 1
    3 1
    . 0
    3 1
    4 1
    . 0
    . 0
    . 0
    3 1
    3 1
    4 1
    . 0
    2 0
    5 1
    3 1
    2 0
    . 0
    2 1
    . 0
    2 1
    5 1
    4 1
    2 1
    . 0
    3 1
    4 1
    . 0
    3 1
    . 0
    3 1
    5 1
    3 1
    . 0
    3 1
    4 1
    3 1
    3 1
    5 1
    3 1
    3 1
    . 0
    5 1
    3 1
    4 1
    . 0
    . 0
    . 0
    4 1
    3 1
    . 0
    . 0
    3 1
    . 0
    3 1
    4 1
    . 0
    4 1
    end
    label values age_cat labelage
    label def labelage 2 "De 20 a 29 años", modify
    label def labelage 3 "De 30 a 39 años", modify
    label def labelage 4 "De 40 a 49 años", modify
    label def labelage 5 "De 50 a 59 años", modify
    Here is the syntax I'm currently using (the variable labels are in spanish):

    Code:
    catplot age_cat if submission==1, percentage var1opts(gap(10) relabel(1 "Menos de 20 años" 2 "20-29 años" 3 "30-39 años" 4 "40-49 años" 5 "50-59 años" 6 "60 años o más")) blabel(bar, position(outside) format (%9.1f) color (black) size(medium)) bar(1, bfcolor(50 43 154) blcolor(50 43 154)) bar(2, bfcolor(50 43 154) blcolor(50 43 154)) bar(3, bfcolor(50 43 154) blcolor(50 43 154)) bar(4,bfcolor(50 43 154) blcolor(50 43 154)) bar(5, bfcolor(50 43 154) blcolor(50 43 154)) bar(6, bfcolor(50 43 154) blcolor(50 43 154)) b1title("") ytitle("Porcentaje (%)", size (medium)) ylabel(0(20)100, labsize(small) nogrid) legend(off) asyvars plotregion(fcolor(white)) graphregion(fcolor(white) lcolor(white)) legend(region(lcolor(white))) recast(bar)
    With this code I get the attached graphic. I think what I am asking may be very simple, however I have been trying to specify the labels with no result.

    If anyone knows what the problem is I would appreciate it.

    Thanks in advance.

    Best regards,

    Helena
    Attached Files
    Last edited by helena gonzalez; 04 Jan 2024, 04:49.

  • #2
    I am the author of catplot and found your code hard to follow. That's a fact, not a criticism, and arises mostly because in your case catplot is a wrapper for graph bar. Being a wrapper is intended to be a feature, as graph bar does most of the work and the main point of catplot is just to do some calculations first. Indeed the catplot code is not long or complicated. But in your case, your use of graph bar options complicates calculations and graphics with puzzling results.

    I have not worked out everything that went wrong, but I have identified some problems and (I think) worked out how to get a graph that you want, or at least that you should like more than what you got.

    The history here is relevant. catplot was first (2003) a reaction to the fact that in the very new graphics of Stata 8 certain simple bar charts were harder to produce than I wanted. I rewrote the syntax in 2010 for reasons not important here.

    Later (within Stata 13) official support for bar charts was improved, but after a decade some people had got used to catplot and (regardless of that) the syntax of catplot seemed congenial to some -- or perhaps people were not all aware of the new official support.

    Down to details:

    1. The percentage option you used is an option of graph bar. catplot expects you to use percent or percent() for percentages.

    2. relabel() doesn't work as you may think. The first argument of relabel() is the first bar shown on the plot, which need not be the bar for a category with value 1. And so on. But I have not identified why it produces no result.

    3. asyvars is sometimes essential when you want different colours for different bars. But you don't want different colours. So some of your code is asking for something and then trying to reverse its effects.

    4. I can't see the point of showing the entire scale to 100%, or of making the labels small, or even of showing axis labels when the bars show percents.

    More is going on within your code, but I will stop there.

    The graph here is naturally not a reproduction of yours in #1. In the data example only categories 2 3 4 5 are presented; there are no values for 1 and 6. No matter; the commands here should work for the full dataset.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float age_cat byte submission
    . 0
    . 0
    . 0
    . 0
    2 1
    2 1
    . 0
    3 1
    2 1
    4 1
    3 1
    3 1
    4 1
    5 1
    4 1
    4 1
    3 1
    4 1
    . 0
    4 1
    5 1
    . 0
    2 1
    . 0
    . 0
    . 0
    3 1
    2 1
    3 1
    3 1
    3 1
    2 1
    . 0
    . 0
    4 1
    . 0
    4 1
    2 0
    . 1
    3 0
    4 1
    4 1
    2 1
    3 1
    3 1
    . 0
    3 1
    4 1
    . 0
    . 0
    . 0
    3 1
    3 1
    4 1
    . 0
    2 0
    5 1
    3 1
    2 0
    . 0
    2 1
    . 0
    2 1
    5 1
    4 1
    2 1
    . 0
    3 1
    4 1
    . 0
    3 1
    . 0
    3 1
    5 1
    3 1
    . 0
    3 1
    4 1
    3 1
    3 1
    5 1
    3 1
    3 1
    . 0
    5 1
    3 1
    4 1
    . 0
    . 0
    . 0
    4 1
    3 1
    . 0
    . 0
    3 1
    . 0
    3 1
    4 1
    . 0
    4 1
    end
    label values age_cat labelage
    
    label def labelage 1 "Menos de 20 años" 2 "20-29 años" 3 "30-39 años" 4 "40-49 años" 5 "50-59 años" 6 "60 años o más"
    
    catplot age_cat if submission==1, percent recast(bar)  bar(1, bfcolor(50 43 154) blcolor(50 43 154)) ///
    var1opts(gap(10)) blabel(bar, position(outside) format(%9.1f) color (black) size(medium)) ///  
    ytitle("Porcentaje (%)") b1title("") yla(none) ///
    plotregion(fcolor(white)) graphregion(fcolor(white) lcolor(white))
    Click image for larger version

Name:	age_catplot.png
Views:	1
Size:	28.7 KB
ID:	1738848

    Last edited by Nick Cox; 04 Jan 2024, 06:35.

    Comment


    • #3
      Thank you very much Nick for your quick reply, I have been able to make the graph that I wanted. Thank you very much!

      I will look into changing and improving the code for my graphs, since I didn't realize that there were some mistakes. Thank you very much for your insights.

      Kind regards,

      Helena

      Comment


      • #4
        It's worth pointing out that with a little bit of work histogram can do a good job too. For your full dataset, try something like

        Code:
        histogram age_cat if submission==1, percent barw(0.9) bfcolor("50 43 154") blcolor("50 43 154") ///
        addlabels addlabopts(mlabformat(%2.1f) mlabsize(medium)) discrete xla(1/6, valuelabel tlcolor(none)) xsc(r(0.5 6.5)) ytitle("Porcentaje (%)") xtitle("") yla(none) ///
        plotregion(fcolor(white)) graphregion(fcolor(white) lcolor(white))

        Comment

        Working...
        X