Announcement

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

  • Merging more graphs by different categories

    Dear all, I have some datasets about the reasons for abstention in Europe. For each possible answer I have the share of people that indicate it as a possible reason (people can put more reasons) and a dummy variable which groups responses into three categories (indifference, protest, involuntariness). I put here a part of the data

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str28 Answer int Year double ShareItaly byte Reason
    "Against Europe" 2004    0 3
    "Against Europe" 2019    3 3
    "Against Europe" 2009    4 3
    "Against Europe" 2014    3 3
    "Away From Home" 2019 14.5 2
    "Away From Home" 2009 11.1 2
    "Away From Home" 2004 14.2 2
    "Away From Home" 2014   17 2
    "I Rarely Vote"  2014    9 1
    "I Rarely Vote"  2004  9.4 1
    "I Rarely Vote"  2019   10 1
    "I Rarely Vote"  2009  7.1 1
    end
    I have four different survey years. I want to create an horizontal graph bar for each year and then combine them. Moreover I want that all answers in a given group are coloured equally. I enclose an example of how I would like to obtain the graph.
    Click image for larger version

Name:	image_31312.png
Views:	1
Size:	50.0 KB
ID:	1716098



    I tried using the command (trying to change the colour of only the first two bars)

    Code:
     graph hbar ShareItaly, over(Answer) bar(1, fcolor(red)) bar(2, fcolor(blue)) by(Year) bargap(0.5)
    but in each year the categories are repeated, and I only colour all bars red. Thank you all in advance
    Last edited by Francesco Migliore; 05 Jun 2023, 05:57.

  • #2
    I can't follow the colour rules in your displayed graph or your own colour preferences in which the code asks for red and blue and the text seems to imply that you really want red.

    Otherwise here is some technique. labmask is from the Stata Journal.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str28 Answer int Year double ShareItaly byte Reason
    "Against Europe" 2004    0 3
    "Against Europe" 2019    3 3
    "Against Europe" 2009    4 3
    "Against Europe" 2014    3 3
    "Away From Home" 2019 14.5 2
    "Away From Home" 2009 11.1 2
    "Away From Home" 2004 14.2 2
    "Away From Home" 2014   17 2
    "I Rarely Vote"  2014    9 1
    "I Rarely Vote"  2004  9.4 1
    "I Rarely Vote"  2019   10 1
    "I Rarely Vote"  2009  7.1 1
    end
    
    su ShareItaly
    gen Toshow = Year + 3.5 * ShareItaly / r(max)
    labmask Reason, values(Answer)
    twoway rbar Year Toshow Reason, horizontal barw(0.8) xla(2004(5)2019, tlc(none) ang(-0.001)) || scatter Reason Toshow, yla(1/3, noticks valuelabel) ms(none) mla(ShareItaly) mlabpos(3) mlabformat(%2.1f) legend(off) xsc(r(. 2023)) ytitle("")
    Click image for larger version

Name:	italy.png
Views:	1
Size:	26.6 KB
ID:	1716117

    Comment


    • #3
      Thank you very much Nick Cox for the suggestion. The graph works perfectly. The problem with the colours does not relate specifically to blue and red as much as to grouping. In the previous question, I only loaded three types of Answer. In my complete dataset there are about twenty. These are then divided into three groups according to the variable Reason. My aim would be to have the graph you suggested, but that all bars in a given group were the same color.

      Comment


      • #4
        IIUC, one way forward is to use separate to separate the outcome variable. Then different colours are automatic, and may be tuned.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str28 Answer int Year double ShareItaly byte Reason
        "Against Europe" 2004    0 3
        "Against Europe" 2019    3 3
        "Against Europe" 2009    4 3
        "Against Europe" 2014    3 3
        "Away From Home" 2019 14.5 2
        "Away From Home" 2009 11.1 2
        "Away From Home" 2004 14.2 2
        "Away From Home" 2014   17 2
        "I Rarely Vote"  2014    9 1
        "I Rarely Vote"  2004  9.4 1
        "I Rarely Vote"  2019   10 1
        "I Rarely Vote"  2009  7.1 1
        end
        
        su ShareItaly
        gen Toshow = Year + 3.5 * ShareItaly / r(max)
        
        separate Toshow, by(Reason) veryshortlabel 
        labmask Reason, values(Answer)
        
        twoway rbar Year Toshow1 Reason, horizontal barw(0.8) xla(2004(5)2019, tlc(none) ang(-0.001)) ///
        || rbar Year Toshow2 Reason, horizontal barw(0.8) xla(2004(5)2019, tlc(none) ang(-0.001)) ///
        || rbar Year Toshow3 Reason, horizontal barw(0.8) xla(2004(5)2019, tlc(none) ang(-0.001)) ///
        || scatter Reason Toshow, yla(1/3, noticks valuelabel) ms(none) mla(ShareItaly) mlabpos(3) mlabcolor(black) mlabformat(%2.1f) legend(off) xsc(r(. 2023)) ytitle("")
        Click image for larger version

Name:	threecolours.png
Views:	1
Size:	26.3 KB
ID:	1716420



        The principle and a different application were explained at https://www.stata-journal.com/articl...article=gr0023 -- which also is the documentation for the veryshortlabel option.

        Comment

        Working...
        X