Search code examples
stata

Relabel using grc1leg


I am trying to show four different graph plots in one illustration. I am using the community-contributed command grc1leg. However, if I use relabel in each separate graph the command grc1leg does not recognise the changes.

An example of my data is:

+---------+----------+------------+----+----+----+----+
| student | matutino | vespertino | p6 | p7 | p8 | p9 |
+---------+----------+------------+----+----+----+----+
| 1       | 1        | 0          | 1  | 3  | 4  | 3  |
+---------+----------+------------+----+----+----+----+
| 2       | 1        | 0          | 2  | 5  | 1  | 2  |
+---------+----------+------------+----+----+----+----+
| 3       | 0        | 1          | 2  | 2  | 6  | 2  |
+---------+----------+------------+----+----+----+----+

Variable Matutino is a dummy of whether the student attends morning classes.

Variable Vespertino is a dummy of whether the student attends afternoon classes.

Variable p6 refers to question 6 which asks if the student works and it takes two values: 1 and 2

Variable p7 asks how long the student has been working and it takes six values: 1,2,3,4,5,6

Variables p8 and p9 are just like p7.

My data is coded in numerical form, however it represents string answers from a survey. I want to have in the x-axis of graphs the string names instead of the numerical code.

My code is the following:

graph bar (percent) matutino (percent) vespertino, over (p6,relabel (1 "Si" 2 "No")) name(p6, replace) title("¿Trabajas actualmente?") nolabel
graph bar (percent) matutino (percent) vespertino, over (p7,relabel (1 "0-1" 2 "1-5" 3 "6-11" 4 "12-24" 5 "más de 24")) name(p7, replace) title ("Tiempo trabajando") legend(off) b1title("Meses")
graph bar (percent) matutino (percent) vespertino, over (p8,relabel (1 "1-3" 2 "4-6" 3 "7-9" 4 "10-12" 5 "13-15" 6 "16-18" 7 "19-21" 8 "22-24" 9 "más de 25")) name(p8, replace) title ("Horas trabajadas a la semana") legend(off) b1title("Horas")
graph bar (percent) matutino (percent) vespertino, over (p9,relabel (1 "Independencia" 2 "Gastos personales" 3 "Continuar estudiando" 4 "Experiencia laboral" 5 "Mantener a la familia" 6 "Ayuda en el negocio familiar") label (labsize(small) angle(45))) name(p9, replace) title ("Motivo para trabajar") legend(off)
grc1leg p6 p7 p8 p9, l1(Porcentaje) legendfrom(p6)

The code works perfectly for each graph separately, for example:

graph bar (percent) matutino (percent) vespertino, over (p7,relabel (1 "0-1" 2 "1-5" 3 "6-11" 4 "12-24" 5 "más de 24")) name(p7, replace) title ("Tiempo trabajando") legend(off) b1title("Meses")

And the code also works when I combine the four graphs but without the relabel option, for example:

graph bar (percent) matutino (percent) vespertino, over (p6) name(p6, replace) title("¿Trabajas actualmente?") nolabel
graph bar (percent) matutino (percent) vespertino, over (p7) name(p7, replace) title ("Tiempo trabajando") legend(off) b1title("Meses")
graph bar (percent) matutino (percent) vespertino, over (p8) name(p8, replace) title ("Horas trabajadas a la semana") legend(off) b1title("Horas")
graph bar (percent) matutino (percent) vespertino, over (p9) name(p9, replace) title ("Motivo para trabajar") legend(off)
grc1leg p6 p7 p8 p9, l1(Porcentaje) legendfrom(p6)

What I want is to have the four graphs in the same illustration but using the string labels I wrote down in each separate graph.

I have also tried the label define option but it doesn't work when displaying the graphs. The only solution I have found is creating new variables for each question using the string names. However, I have 154 questions and I think there should be an easier way.

Cross posted on Statalist.


Solution

  • Re-labeling does not work because grc1leg draws again each graph before it combines them.

    The easiest way to deal with this problem is to combine the four graphs without the relabel option and then post-process the produced graph as follows:

    local lbl 1-3 4-6 7-9
    tokenize `lbl'
    
    forvalues i = 1 / 3 {
        gr_edit .plotregion1.graph3.grpaxis.edit_tick `i' ///
        `.Graph.plotregion1.graph3.grpaxis.major.dlg_tickpos[`i']' ///
        `"``i''"', tickset(major)
    }
    

    The idea is to define a local macro lbl, which holds the labels and then loop over the tick positions to re-label them. The above code modifies the third graph in this case, but the process for the remaining graphs is the same.