Announcement

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

  • Help with graphing % of frequencies of two different variables on one graph

    Hello Statalist,

    I am a new Stata user (using Stata 15). I am attempting to graph the frequencies within a category with 2 different variables on one bar graph.

    I have variable "pindeye" which has 5 possible results. "pindeye" was measured over two years: 2018 and 2019 (variable year). Without actually splitting the data, I was wondering if it is possible to graph the frequencies of the 5 possible results of "pindeye" for 2018 and again for 2019, but to combine these on one graph.

    . graph bar, by(year) over(pindeye) asyvars bar(1, bfcolor(blue*0.5)) bar(2, bfcolor(green*0.5))
    This gives me the following, which displays the correct data, but not overlapping
    Click image for larger version

Name:	Graph1.png
Views:	1
Size:	19.1 KB
ID:	1512275



    graph bar, over(year) over(pindeye) asyvars bar(1, bfcolor(blue*0.5)) bar(2, bfcolor(green*0.5))
    This gives me the visual representation I am attempting to achieve, but gives me the wrong data (it gives the overall frequency within the category of variable "pindeye" (it doesn't allow for calculating frequency separately for 2018 and 2019)
    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	18.2 KB
ID:	1512276



    I understand a simple fix for this would be to split the data for 2018 and 2019 into their own variables, but as I have many variables beyond "pindeye" that I am testing, I feel like that would be somewhat time consuming. I have searched the forums and reviewed the manuals I have access to and haven't found a command line combination to achieve the above goal. I appreciate any assistance with this. Thank you!

  • #2
    There is no data example here (please note FAQ Advice #12) but fortuitously and fortunately Stata's auto data can be adapted to give an analogue for your problem.

    catplot from SSC was written because graph in Stata 8 did not seem to offer easy handles for this kind of graph. Stata caught up somewhat a few versions later but for a variety of reasons I remain more fluent with catplot. You must install catplot before you can use it. The installation command is commented out below, because I already have it installed.

    This seems to provide the flexibility you seek. A strong personal slogan is Kill the key! Lose the legend! (if you can) and that is possible here. recast(bar) is available if you insist on vertical bars.


    Code:
    sysuse auto, clear
    label def year 0 "2018" 1 "2019"
    label val foreign year 
    label def answer 1 "not at all" 2 "a little"  3 "50/50" 4 "for the most part" 5 "completely" 
    label val rep78 answer 
    
    set scheme s1color 
    * ssc install catplot 
    
    catplot rep78 foreign, percent(foreign) blabel(bar, format(%2.1f)) name(G1, replace) ///
    subtitle(% in each year) 
    
    catplot foreign rep78, percent(foreign) blabel(bar, format(%2.1f)) name(G2, replace) ///
    subtitle(% in each year) 
    
    graph combine G1 G2
    Click image for larger version

Name:	anothercatplot.png
Views:	1
Size:	27.7 KB
ID:	1512291

    Comment

    Working...
    X