Announcement

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

  • Figure with bar charts sorted in descending order: How to assign the values of one variable as the value labels for another variable?

    Hi Statalanders,

    In general, to get a -twoway bar yvar xvar, horizontal- in descending order of the xvar, we plot instead a sorting variable of xvar and add the option -sort-. Now, if we want to label the yvar values, -labmask- comes handy to getting things done (credits to Nick Cox and UCLA ARC). But, how could we adapt this process when -twoway bar, hor over()- is used to get a figure with several charts? In this case, it is likely that the relationship between xvar and yvar differs among graphs, and thus the labels of sorting variable.

    Example:

    Data:
    Code:
    clear all
    
    input str5 nuts2 year cwr_nuts2 lo hi nuts2_sort
    AS 1971 319 319 319 1
    AR 1971 324 288 334 2
    GA 1971 326 270 381 3
    RI 1971 328 328 328 4
    CL 1971 336 290 396 5
    CB 1971 363 363 363 6
    CT 1971 367 328 374 7
    NC 1971 373 373 373 8
    EX 1971 385 364 400 9
    CM 1971 386 340 436 10
    MD 1971 392 392 392 11
    VC 1971 392 352 423 12
    IB 1971 393 393 393 13
    PV 1971 398 392 420 14
    MC 1971 459 459 459 15
    AN 1971 460 434 513 16
    CE-ML 1971 469 459 479 17
    CN 1971 512 469 560 18
    CL 1980 306 266 367 1
    AR 1980 306 279 311 2
    AS 1980 314 314 314 3
    GA 1980 331 253 387 4
    EX 1980 332 311 345 5
    NC 1980 337 337 337 6
    RI 1980 339 339 339 7
    PV 1980 339 335 366 8
    CM 1980 340 293 369 9
    CT 1980 352 317 374 10
    IB 1980 353 353 353 11
    CB 1980 353 353 353 12
    MD 1980 364 364 364 13
    VC 1980 380 362 395 14
    CE-ML 1980 386 352 414 15
    CN 1980 406 398 415 16
    AN 1980 407 359 458 17
    MC 1980 424 424 424 18
    PV 1990 165 163 179 1
    AS 1990 169 169 169 2
    CT 1990 190 185 214 3
    NC 1990 190 190 190 4
    AR 1990 192 184 231 5
    MD 1990 195 195 195 6
    GA 1990 195 177 208 7
    CB 1990 197 197 197 8
    RI 1990 198 198 198 9
    CL 1990 199 179 225 10
    VC 1990 218 206 241 11
    IB 1990 236 236 236 12
    CN 1990 245 234 256 13
    CM 1990 258 232 269 14
    MC 1990 266 266 266 15
    AN 1990 268 252 291 16
    EX 1990 275 261 284 17
    CE-ML 1990 319 308 332 18
    AS 2000 119 119 119 1
    GA 2000 138 125 150 2
    CB 2000 140 140 140 3
    PV 2000 147 138 162 4
    CL 2000 148 136 181 5
    RI 2000 163 163 163 6
    AR 2000 164 161 175 7
    MD 2000 166 166 166 8
    CT 2000 174 172 184 9
    VC 2000 176 170 183 10
    NC 2000 180 180 180 11
    CN 2000 186 177 193 12
    IB 2000 190 190 190 13
    EX 2000 201 188 208 14
    AN 2000 202 192 222 15
    CM 2000 203 192 207 16
    MC 2000 210 210 210 17
    CE-ML 2000 271 255 289 18
    AS 2010 164 164 164 1
    GA 2010 174 146 188 2
    CL 2010 181 151 204 3
    CN 2010 188 185 191 4
    EX 2010 199 189 205 5
    CB 2010 200 200 200 6
    PV 2010 207 198 222 7
    AR 2010 211 195 214 8
    IB 2010 214 214 214 9
    MD 2010 217 217 217 10
    RI 2010 219 219 219 11
    CM 2010 220 188 241 12
    VC 2010 221 216 225 13
    NC 2010 228 228 228 14
    AN 2010 230 209 244 15
    CT 2010 233 230 247 16
    MC 2010 250 250 250 17
    CE-ML 2010 308 294 323 18
    CN 2020 150 150 150 1
    AS 2020 154 154 154 2
    GA 2020 167 153 171 3
    CB 2020 172 172 172 4
    CL 2020 176 151 189 5
    IB 2020 183 183 183 6
    EX 2020 187 178 191 7
    VC 2020 191 188 201 8
    MD 2020 194 194 194 9
    AR 2020 195 194 203 10
    PV 2020 196 189 215 11
    RI 2020 197 197 197 12
    CM 2020 198 182 210 13
    CT 2020 199 195 214 14
    AN 2020 200 188 238 15
    NC 2020 210 210 210 16
    MC 2020 222 222 222 17
    CE-ML 2020 286 242 329 18
    AR 1971 324 288 334 2
    GA 1971 326 270 381 3
    RI 1971 328 328 328 4
    CL 1971 336 290 396 5
    CB 1971 363 363 363 6
    CT 1971 367 328 374 7
    NC 1971 373 373 373 8
    EX 1971 385 364 400 9
    CM 1971 386 340 436 10
    MD 1971 392 392 392 11
    VC 1971 392 352 423 12
    IB 1971 393 393 393 13
    PV 1971 398 392 420 14
    MC 1971 459 459 459 15
    AN 1971 460 434 513 16
    CE-ML 1971 469 459 479 17
    CN 1971 512 469 560 18
    CL 1980 306 266 367 1
    AR 1980 306 279 311 2
    AS 1980 314 314 314 3
    GA 1980 331 253 387 4
    EX 1980 332 311 345 5
    NC 1980 337 337 337 6
    RI 1980 339 339 339 7
    PV 1980 339 335 366 8
    CM 1980 340 293 369 9
    CT 1980 352 317 374 10
    IB 1980 353 353 353 11
    CB 1980 353 353 353 12
    MD 1980 364 364 364 13
    VC 1980 380 362 395 14
    CE-ML 1980 386 352 414 15
    CN 1980 406 398 415 16
    AN 1980 407 359 458 17
    MC 1980 424 424 424 18
    PV 1990 165 163 179 1
    AS 1990 169 169 169 2
    CT 1990 190 185 214 3
    NC 1990 190 190 190 4
    AR 1990 192 184 231 5
    MD 1990 195 195 195 6
    GA 1990 195 177 208 7
    CB 1990 197 197 197 8
    RI 1990 198 198 198 9
    CL 1990 199 179 225 10
    VC 1990 218 206 241 11
    IB 1990 236 236 236 12
    CN 1990 245 234 256 13
    CM 1990 258 232 269 14
    MC 1990 266 266 266 15
    AN 1990 268 252 291 16
    EX 1990 275 261 284 17
    CE-ML 1990 319 308 332 18
    AS 2000 119 119 119 1
    GA 2000 138 125 150 2
    CB 2000 140 140 140 3
    PV 2000 147 138 162 4
    CL 2000 148 136 181 5
    RI 2000 163 163 163 6
    AR 2000 164 161 175 7
    MD 2000 166 166 166 8
    CT 2000 174 172 184 9
    VC 2000 176 170 183 10
    NC 2000 180 180 180 11
    CN 2000 186 177 193 12
    IB 2000 190 190 190 13
    EX 2000 201 188 208 14
    AN 2000 202 192 222 15
    CM 2000 203 192 207 16
    MC 2000 210 210 210 17
    CE-ML 2000 271 255 289 18
    AS 2010 164 164 164 1
    GA 2010 174 146 188 2
    CL 2010 181 151 204 3
    CN 2010 188 185 191 4
    EX 2010 199 189 205 5
    CB 2010 200 200 200 6
    PV 2010 207 198 222 7
    AR 2010 211 195 214 8
    IB 2010 214 214 214 9
    MD 2010 217 217 217 10
    RI 2010 219 219 219 11
    CM 2010 220 188 241 12
    VC 2010 221 216 225 13
    NC 2010 228 228 228 14
    AN 2010 230 209 244 15
    CT 2010 233 230 247 16
    MC 2010 250 250 250 17
    CE-ML 2010 308 294 323 18
    CN 2020 150 150 150 1
    AS 2020 154 154 154 2
    GA 2020 167 153 171 3
    CB 2020 172 172 172 4
    CL 2020 176 151 189 5
    IB 2020 183 183 183 6
    EX 2020 187 178 191 7
    VC 2020 191 188 201 8
    MD 2020 194 194 194 9
    AR 2020 195 194 203 10
    PV 2020 196 189 215 11
    RI 2020 197 197 197 12
    CM 2020 198 182 210 13
    CT 2020 199 195 214 14
    AN 2020 200 188 238 15
    NC 2020 210 210 210 16
    MC 2020 222 222 222 17
    CE-ML 2020 286 242 329 18
    end
    
    label var cwr_nuts2 "CWR by NUTS2 (Autonomous Communities and Cities, N=18)"
    Now, plot -cwr_nuts2- by -nuts2 and year- in descending order.

    Code:
    * The following figure shows a more appropiate legend to read the graphs,
    * but the numbers are not labeled with the region names
    encode nuts2, gen(nuts2_num)
    levelsof nuts2_num, local(nuts2)
    
    twoway bar cwr nuts2_sort, hor sort by(year,    ///
        ixaxes ixtick ixlab    iy iyt iyl                 ///
        ti("Child-Woman Ratio, Spain", si(medsmall)) ///
        note(""))                                     ///
        mlabsize(vsmall) mlabc(black) mlabpos(6)      ///
        fcolor(blue%25) lc(black)                      ///
        || rcap lo hi nuts2_sort, hor                 ///
        xtitle("") xla(0(50)600, labs(vsmall) tlc(black) tp(o) nogrid)  ///
        ytitle("") ysc(noline) yla(`nuts2', val labs(vsmall) tlc(black) tp(o)) ///
        legend(order(1 2) si(vsmall) pos(6) row(1) ///
            label(2 "CWR by NUTS3 (Provinces, Islands, Ceuta and Melilla, N=59)")) plotr(lc(black))
    graph export "esp_cwr_var_7120_a.png", replace





    Problem: The values of the sorting variable (-nuts2_sort-) often correspond to different regions depending on the year. Ideally, in each graph, the label values of -nuts2_sort- should be the values of the string -nuts2-.

    Transitory solution:
    1) Generate a chart for each year, assigning the values of -nuts2_num- as the value labels for -nuts2_sort-. Credits: https://stats.oarc.ucla.edu/stata/fa...ther-variable/
    2) Combine all graphs in one figure.

    Code:
    levelsof year, local(years)
    foreach yy of local years {
        preserve
            keep if year==`yy'
            
            levelsof nuts2_num, local(nuts2)
            labmask nuts2_sort if year==`yy', values(nuts2_num) decode
            
            twoway bar cwr nuts2_sort if year==`yy', hor sort         ///
                mlabsize(vsmall) mlabc(black) mlabpos(6)      ///
                fcolor(blue%25) lc(black)                      ///
                || rcap lo hi nuts2_sort if year==`yy', hor    ///
                xti("") xla(0(50)600, labs(vsmall) tlc(black) tp(o))  ///
                ytitle("") ysc(noline) yla(`nuts2', val labs(vsmall) tlc(black) tp(o) nogrid) ///
                leg(off) plotr(lc(black)) ///
                ti("`yy'", si(medsmall)) name(g`yy')
        restore
    }
    
    graph combine g1971 g1980 g1990 g2000 g2010 g2020,    ///
        ti("Child-Woman Ratio, Spain", si(medsmall))    ///
        subti("Blue bars indicate CWR by NUTS2 (N=19)" ///
              "Orange lines indicate min and max values of CWR by NUTS3 (N=59)", si(vsmall))
    graph export "esp_cwr_var_7120_b.png", replace
    [ATTACH=CONFIG]n1715826[/ATTACH]







    Problem: The transitory solution doesn't preserve the legend and we have to add a subtitle or note to explain bars and lines.

    Any ideas and comments welcome! Thanks!
    Last edited by Emanuel Agu; 01 Jun 2023, 13:45.
Working...
X