Announcement

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

  • How to change the look of a subset of bars using specific criteria - bar graphs

    Dear all,

    I am working with a dataset that contains the number of candidates for each call of a study abroad program and I prepared the following graph:

    Code:
        #delimit ;
            graph bar (asis) number_of_candidates_per_call,
                over(call_number, sort(1) descending label(labsize(*0.4)))
                nofill
                linetype(line) lines(lw(vthin) lc(gs12))
                by(call_year, title("Number of candidates per call", placement(center)))
                subtitle("",  nobox  fcolor(none) nobexpand pos(9))
                ysc(alt)  
                horiz
                yla(, nogrid format(%10.0fc) labsize(*0.5))
                blabel(bar, format(%9.0fc) size(*0.4))
            scheme(s1color)
        ;
        #delimit cr
    Click image for larger version

Name:	graph_attempt_before_statalist.png
Views:	1
Size:	52.0 KB
ID:	1592220


    I would like to

    (i) change the color (to red) of bars associated with a subset of four different countries: 'USA', 'UK', 'POR' and 'CAN'.
    Obs: There are 21 different countries in the dataset. The variable for the acronym of the destination countries is called 'dest_country'.

    (ii) place a label containing the destination country acronym just to the right of the bar only for the aforementioned subset of bars (associated with the values 'USA', 'UK', 'POR' and 'CAN' of the variable 'dest_country').

    (iii) include a "title" for each panel according to the variable 'call_year' (it assumes the following values: 2011, 2012, 2013 and 2014) so that one could understand to which year each panel refers.

    You can find below the code to import the dataset.

    Each observation is a call and each call is linked to one country.

    Can you help me with that?

    Thank you very much.

    Any help is greatly appreciated.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long call_number int call_year float number_of_candidates_per_call str3 dest_country_acronym
    19 2013 180 "USA"
     9 2012 141 "POR"
    31 2013 119 "USA"
     7 2012 101 "UK"
    26 2013  98 "UK"
    42 2013  90 "UK"
    51 2014  88 "USA"
    64 2014  73 "UK"
    25 2013  60 "CAN"
     2 2012  53 "USA"
    32 2013  38 "GER"
    22 2013  36 "HGR"
     4 2012  31 "AUS"
     8 2012  31 "SPN"
    36 2013  31 "IRE"
    59 2014  31 "CAN"
    20 2013  30 "GER"
     3 2012  29 "GER"
    61 2014  28 "SPN"
    49 2013  28 "SPN"
    24 2013  25 "AUS"
    56 2014  21 "AUS"
    67 2014  20 "IRE"
    55 2014  19 "AUS"
    52 2014  19 "GER"
    41 2013  16 "CAN"
    33 2013  15 "FRA"
    34 2013  14 "ITL"
    28 2013  14 "AUS"
     6 2012  14 "NL"
    18 2013  13 "IRE"
    23 2013  13 "CAN"
    30 2013  12 "NEZ"
    62 2014  11 "NL"
    38 2013  11 "HGR"
    54 2014  11 "ITL"
    58 2014  11 "BEL"
    68 2014  10 "HGR"
    10 2012  10 "FRA"
    48 2013   9 "NL"
    27 2013   9 "CAN"
    60 2014   9 "CAN"
    40 2013   9 "AUS"
    44 2013   7 "AUS"
    43 2013   7 "CAN"
    11 2012   7 "HGR"
    12 2012   6 "ITL"
    29 2013   5 "FIN"
    57 2014   4 "BEL"
    35 2013   4 "NRW"
     1 2011   4 "USA"
    53 2014   4 "FRA"
    69 2014   2 "JPN"
    50 2014   2 "POL"
    17 2013   2 "CHI"
    37 2013   2 "CHI"
    45 2013   2 "FIN"
    46 2013   2 "NEZ"
    47 2013   2 "BEL"
    21 2013   2 "JPN"
    65 2014   1 "SWE"
    39 2013   1 "JPN"
    63 2014   1 "NEZ"
    14 2012   1 "SWE"
     5 2012   1 "SKO"
    66 2014   1 "NRW"
    13 2012   1 "USA"
    15 2012   1 "NRW"
    16 2012   1 "JPN"
    end
    label values call_number no_chamada2
    label def no_chamada2 1 "101", modify
    label def no_chamada2 2 "117", modify
    label def no_chamada2 3 "118", modify
    label def no_chamada2 4 "119", modify
    label def no_chamada2 5 "121", modify
    label def no_chamada2 6 "122", modify
    label def no_chamada2 7 "123", modify
    label def no_chamada2 8 "126", modify
    label def no_chamada2 9 "127", modify
    label def no_chamada2 10 "128", modify
    label def no_chamada2 11 "129", modify
    label def no_chamada2 12 "130", modify
    label def no_chamada2 13 "132", modify
    label def no_chamada2 14 "133", modify
    label def no_chamada2 15 "134", modify
    label def no_chamada2 16 "135", modify
    label def no_chamada2 17 "136", modify
    label def no_chamada2 18 "138", modify
    label def no_chamada2 19 "143", modify
    label def no_chamada2 20 "144", modify
    label def no_chamada2 21 "145", modify
    label def no_chamada2 22 "146", modify
    label def no_chamada2 23 "147", modify
    label def no_chamada2 24 "148", modify
    label def no_chamada2 25 "149", modify
    label def no_chamada2 26 "151", modify
    label def no_chamada2 27 "152", modify
    label def no_chamada2 28 "153", modify
    label def no_chamada2 29 "154", modify
    label def no_chamada2 30 "155", modify
    label def no_chamada2 31 "156", modify
    label def no_chamada2 32 "157", modify
    label def no_chamada2 33 "158", modify
    label def no_chamada2 34 "159", modify
    label def no_chamada2 35 "161", modify
    label def no_chamada2 36 "162", modify
    label def no_chamada2 37 "163", modify
    label def no_chamada2 38 "164", modify
    label def no_chamada2 39 "165", modify
    label def no_chamada2 40 "167", modify
    label def no_chamada2 41 "168", modify
    label def no_chamada2 42 "170", modify
    label def no_chamada2 43 "171", modify
    label def no_chamada2 44 "172", modify
    label def no_chamada2 45 "173", modify
    label def no_chamada2 46 "174", modify
    label def no_chamada2 47 "176", modify
    label def no_chamada2 48 "177", modify
    label def no_chamada2 49 "178", modify
    label def no_chamada2 50 "179", modify
    label def no_chamada2 51 "180", modify
    label def no_chamada2 52 "181", modify
    label def no_chamada2 53 "182", modify
    label def no_chamada2 54 "183", modify
    label def no_chamada2 55 "184", modify
    label def no_chamada2 56 "185", modify
    label def no_chamada2 57 "186", modify
    label def no_chamada2 58 "187", modify
    label def no_chamada2 59 "188", modify
    label def no_chamada2 60 "189", modify
    label def no_chamada2 61 "191", modify
    label def no_chamada2 62 "193", modify
    label def no_chamada2 63 "194", modify
    label def no_chamada2 64 "195", modify
    label def no_chamada2 65 "197", modify
    label def no_chamada2 66 "198", modify
    label def no_chamada2 67 "199", modify
    label def no_chamada2 68 "201", modify
    label def no_chamada2 69 "202", modify
    Last edited by Otavio Conceicao; 31 Jan 2021, 12:59.

  • #2
    A different colour for some bars usually requires a separate variable for each group, either explicitly or through a command doing that for you on the fly.

    It seems to me that separate panels for each year doesn't work very well -- at least the way you did it. Here's another suggestion using labmask and tabplot from the Stata Journal. Note that a sort, here on the number of candidates over all years, shows up the four countries you want to flag in any case.


    Code:
    set scheme s1color
    egen total = total(-number), by(dest)
    egen which = group(total dest)
    labmask which, values(dest)
    gen flag = inlist(des, "USA", "UK", "POR", "CAN")
    tabplot which call_year [fw=number], horizontal showval(offset(0.6)) xtitle("") ytitle("") subtitle("number of candidates per call") separate(flag) bar1(color(blue)) bar2(color(red))
    Click image for larger version

Name:	anothertabplot.png
Views:	1
Size:	34.4 KB
ID:	1592319

    Last edited by Nick Cox; 01 Feb 2021, 05:15.

    Comment


    • #3
      Thank you very much Nick Cox !! Your graph is superb!

      I would like to show all the calls of the program in the same graph because I want to explore the number of calls in each year and ideally just highlight the bars associated with the countries that have the highest number of candidates ('USA', 'UK', 'POR' and 'CAN').

      I was wondering whether it is possible to use some option of 'graph bar' like 'separate' from 'tabplot' to highlight the bars associated with those countries in a 'graph bar' like the one I did.

      Can you help me with that?

      Comment


      • #4
        https://www.stata-journal.com/articl...article=gr0049 covers highlighting.

        SJ11-3 gr0049. Stata tip 102: Highlighting specific bars

        Comment


        • #5
          Great!! Thanks, Nick!!

          Comment


          • #6
            Dear all,

            I managed to change the color of the bars associated with 'USA', which is the main interest.

            I was wondering whether it is possible to remove the labels of the other bars than those associated with 'USA' so that we would only see 'USA' as labels to the right of the red/orange bars.

            Is it possible? Nick Cox

            Thank you!

            Code:
              
                separate number_of_candidates_per_call, by(dest_country_acronym == "USA") 
                ren (number_of_candidates_per_call0 number_of_candidates_per_call1) (other USA)
                
            #delimit ;
                    graph bar (asis) other USA,
                        over(call_number, sort(number_of_candidates_per_call) descending label(labsize(*0.4)))
                        nofill
                        linetype(line) lines(lw(vthin) lc(gs12))
                        by(call_year, note("") title("Number of candidates per call", placement(center)) legend(off))
                        subtitle("", nobox  fcolor(none) nobexpand pos(9))
                        ysc(alt)  
                        horiz
                        yla(, nogrid format(%10.0fc) labsize(*0.5))
                        bar(1, bcolor(green))
                        bar(2, bcolor(red*0.5))
                        nolabel
                        blabel(name, position(outside) gap(2) size(tiny))
                    /* scheme(s1color) */
                ;
                #delimit cr
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	65.1 KB
ID:	1592454

            Comment


            • #7
              https://www.statalist.org/forums/for...ed-year-labels may help. I guess I would start with relabel().


              I'd also recommend trying this out on possible readers before you try to make it public. Apart from finding the varying bar width mighty disconcerting, my reaction would be "So, I am not even allowed to be curious what the others are?".

              Comment


              • #8
                Thank you very much again, Nick!!!!

                Comment


                • #9
                  Hey Nick Cox , you were right, as usual: a 'relabel' within the 'yvaroptions( )' did the job:

                  Thanks!

                  Code:
                  #delimit ;
                          graph bar (asis) other USA,
                              over(call_number, sort(number_of_candidates_per_call) descending label(labsize(*0.4)))
                              yvaroptions(relabel(1 " " 2 "USA")) 
                              /* When we have more han one yvar we the relabel must be used in the 'yvaroptions()' */
                              nofill
                              linetype(line) lines(lw(vthin) lc(gs12))
                              by(call_year, note("") title("Number of candidates per call", placement(center)) legend(off))
                              subtitle("", nobox  fcolor(none) nobexpand pos(9))
                              ysc(alt)  
                              horiz
                              yla(, nogrid format(%10.0fc) labsize(*0.5))
                              bar(1, bcolor(green))
                              bar(2, bcolor(red*0.5))
                              nolabel
                              blabel(name, position(outside) gap(2) size(tiny))
                          /* scheme(s1color) */
                      ;
                      #delimit cr
                  Click image for larger version

Name:	Graph2.png
Views:	1
Size:	52.5 KB
ID:	1593358

                  Comment


                  • #10
                    Hi Nick,
                    In this bar graph, how can I have legend for only one of the colours (Here, USA).
                    Trying to produce bar for mortality rate for 10-14 and 15-19 year olds from different countries. I am highlighting just one country in a different colour. I dont want this country in the legend. But want the other countries 10-14 and 15-19 colour code (bluish and greenish colour) in legend.
                    I am using the following code:

                    graph bar rateAge0both0 rateAge0both1 rateAge1both0 rateAge1both1, over(country, sort(rateAge1both) descending ///
                    label(angle(90) labsize(2.5)) ///
                    gap() ///
                    ) ///
                    ytitle("Crude mortality rate per 100,000") ylabel(, angle(0)) bargap() outergap(0) ///
                    legend(label(1 "Age 10-14") label(3 "Age 15-19") size(vsmall) symysize(vsmall) ring(0) position(1)) /// legend(off) ///
                    bar(1, bcolor(eltgreen)) bar(2, bcolor(green)) bar(3, bcolor(emidblue)) bar(4, bcolor(maroon)) // blabel(total, size(1.5))
                    Last edited by bibha dhungel; 10 Feb 2021, 21:54. Reason: Updating information and my intention

                    Comment


                    • #11
                      The reference in #4 should be the answer to #10. It's hard work otherwise to suggest precise code without either a data example or a graph.

                      Comment

                      Working...
                      X