Announcement

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

  • Moving the bars of a graph

    Hi,

    I am trying to do a bar graph using the following codes:

    * E.1 Question?
    #delimit;
    graph bar (count) [pw=fexp], asyvars percentage over(e1) over(arauc) graphregion(color(white)) bgcolor(white) bargap(25) blabel(group) ysize(5.0) xsize(7.0)
    //legend(label(1 "Sí") label(2 "No") label(3 "No responde") label(4 "No sabe"))
    //ylabel(#5, angle(0) nogrid labsize(small))
    ylabel( 0 "0%" 20 "20%" 40 "40%" 60 "60%" 80 "80%" 100 "100%", nogrid labsize(small) angle(horizontal)) title("")
    yscale(range(0 100) noextend)
    ytitle("")
    //ytitle("%", orientation(horizontal))
    title("Have you cycled?", span color(black))
    //subtitle("Percent of respondents")
    note("Source: XX.")
    blabel(bar, position(outside) format(%9.1f) color(black)) ;
    #delimit cr

    And this is the result I get:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	11.4 KB
ID:	1496317

    However the problem is that I want to have the two "blue categories" together and then the two "red categories", so the results would be like:

    81.1 - 76.3 --------- 18.9 - 23.7

    I have tried changing the order of the over options to:
    graph bar (count) [pw=fexp], asyvars percentage over(arauc) over(e1) graphregion(color(white)) bgcolor(white) bargap(25) blabel(group) ysize(5.0) xsize(7.0)

    But the problem is that the result I get is clearly not the one I want:

    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	11.6 KB
ID:	1496318


    Can you help me, please? Thank you!
    Attached Files

  • #2
    We ask for a reproducible example, please read the FAQs for future posts. Your graph represents 4 categories, which you can recreate yourself and thereafter determine the ordering. Here is an example.

    Code:
    sysuse auto, clear
    set seed 2019
    gen cat= runiformint(0,1)
    label define cat 0 "zero" 1 "one"
    label values cat cat
    *GRAPH 1
    graph bar (count), asyvars percentage over(cat) over(foreign, lab(nolab)) bargap(25) scheme(s1color) leg(off) saving(one)
    
    *CREATE CATEGORIES
    egen gr= group(foreign cat), label
    bys gr: egen total = count(cat)
    bys foreign: gen percent = (total/_N)*100
    
    *GRAPH 2
    graph bar percent, asyvars over(gr, sort(1)) bargap(25) scheme(s1color) leg(off) saving(two)
    gr combine one.gph two.gph
    What remains is to determine what order you want, bar colors and other formatting aspects.


    Graphs:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	22.3 KB
ID:	1496347

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X