Announcement

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

  • Creating separate bar graphs by group


    I'm using hbar to try to create a graph with 8 separate horizontal bar charts, one for each region.

    I can create the graph I want for a single region using
    Code:
    graph hbar (asis) pct_unfilled_by_division if doeregionname == "Central", over(divisionname, sort(1) descending)
    which produces the following graph:

    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	35.0 KB
ID:	1711374


    I want to do a similar graph for each of the 8 regions, and ideally combine them into a single graph. So something like the graph below (copied from another user's 2015 post to the forum) , with the main difference being the graph for each region will have different y axis labels, because each region has its own localities.




    Here is my data:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str12 doeregionname float pct_unfilled_by_division str30 divisionname
    "Central"      16.923077 "Charles City County"           
    "Central"       8.284023 "Sussex County"                 
    "Central"       6.865224 "Petersburg City"               
    "Central"       4.995686 "Prince George County"          
    "Central"       4.868797 "Chesterfield County"           
    "Central"       4.782193 "Henrico County"                
    "Central"      4.7619047 "Surry County"                  
    "Central"      4.1588297 "Richmond City"                 
    "Central"       3.225182 "Hopewell City"                 
    "Central"       1.814443 "Dinwiddie County"              
    "Central"      1.7289073 "Colonial Heights City"         
    "Central"       1.298069 "Powhatan County"               
    "Central"       .8403361 "New Kent County"               
    "Central"       .6323461 "Hanover County"                
    "Central"              0 "Goochland County"              
    "Northern"      7.359307 "Manassas Park City"            
    "Northern"      6.511792 "Prince William County"         
    "Northern"      6.408716 "Madison County"                
    "Northern"      5.793743 "Manassas City"                 
    "Northern"      5.521811 "Culpeper County"               
    "Northern"       4.87271 "Orange County"                 
    "Northern"      4.733003 "Winchester City"               
    "Northern"      4.629316 "Shenandoah County"             
    "Northern"      3.404835 "Warren County"                 
    "Northern"     3.3064265 "Fairfax County"                
    "Northern"     2.9411764 "Rappahannock County"           
    "Northern"     2.8389294 "Alexandria City"               
    "Northern"      2.597558 "Frederick County"              
    "Northern"      1.742286 "Fauquier County"               
    "Northern"      1.571277 "Page County"                   
    "Northern"      1.376601 "Arlington County"              
    "Northern"     1.1442386 "Loudoun County"                
    "Northern"      .7491198 "Clarke County"                 
    "Northern"             0 "Falls Church City"             
    "Nothern Neck" 10.416667 "King and Queen County"         
    "Nothern Neck"  9.123493 "King George County"            
    "Nothern Neck"  8.976661 "Westmoreland County"           
    "Nothern Neck"  8.047891 "Fredericksburg City"           
    "Nothern Neck"  7.571521 "Spotsylvania County"           
    "Nothern Neck"  5.765766 "Caroline County"               
    "Nothern Neck"  5.222614 "Lancaster County"              
    "Nothern Neck" 4.2016807 "Colonial Beach"                
    "Nothern Neck" 3.5137036 "Northumberland County"         
    "Nothern Neck"  3.211304 "Richmond County"               
    "Nothern Neck" 2.7881715 "Stafford County"               
    "Nothern Neck"  2.728699 "King William County"           
    "Nothern Neck"  2.302935 "Gloucester County"             
    "Nothern Neck" 1.9602078 "Middlesex County"              
    "Nothern Neck" 1.2554928 "Mathews County"                
    "Nothern Neck" 1.0072522 "Essex County"                  
    "Nothern Neck"         0 "West Point"                    
    "Southside"    14.092053 "Nottoway County"               
    "Southside"     10.48951 "Prince Edward County"          
    "Southside"     6.934673 "Brunswick County"              
    "Southside"     6.001372 "Lunenburg County"              
    "Southside"     5.665169 "Cumberland County"             
    "Southside"    4.5045047 "Buckingham County"             
    "Southside"     4.316547 "Mecklenburg County"            
    "Southside"     3.418601 "Halifax County"                
    "Southside"    1.7847582 "Amelia County"                 
    "Southside"     1.174743 "Appomattox County"             
    "Southside"     .5947778 "Greensville County"            
    "Southside"            0 "Charlotte County"              
    "Southwest"    10.600707 "Bland County"                  
    "Southwest"     3.733665 "Lee County"                    
    "Southwest"     3.571842 "Dickenson County"              
    "Southwest"    3.3163266 "Tazewell County"               
    "Southwest"     2.887956 "Giles County"                  
    "Southwest"     1.690903 "Grayson County"                
    "Southwest"    1.5967555 "Smyth County"                  
    "Southwest"    1.5154576 "Buchanan County"               
    "Southwest"    1.2872498 "Pulaski County"                
    "Southwest"    1.2543117 "Wise County"                   
    "Southwest"    1.1656694 "Galax City"                    
    "Southwest"    1.0759629 "Carroll County"                
    "Southwest"     1.039249 "Washington County"             
    "Southwest"      .739027 "Wythe County"                  
    "Southwest"     .4830918 "Bristol City"                  
    "Southwest"     .3472584 "Russell County"                
    "Southwest"            0 "Scott County"                  
    "Southwest"            0 "Radford City"                  
    "Southwest"            0 "Norton City"                   
    "Tidewater"    20.830753 "Southampton County"            
    "Tidewater"    16.475494 "Portsmouth City"               
    "Tidewater"    15.176023 "Norfolk City"                  
    "Tidewater"    12.953368 "Franklin City"                 
    "Tidewater"     9.324963 "Newport News City"             
    "Tidewater"     8.720815 "Suffolk City"                  
    "Tidewater"     6.656608 "Northampton County"            
    "Tidewater"     5.902958 "Poquoson City"                 
    "Tidewater"     5.678463 "Hampton City"                  
    "Tidewater"    3.0737975 "Accomack County"               
    "Tidewater"     2.795049 "Chesapeake City"               
    "Tidewater"     2.282772 "Virginia Beach City"           
    "Tidewater"    2.1390374 "York County"                   
    "Tidewater"    1.7799023 "Isle of Wight County"          
    "Tidewater"    1.1473153 "Williamsburg-James City County"
    "Valley"       10.538675 "Lynchburg City"                
    "Valley"       4.5545635 "Bath County"                   
    "Valley"       2.0973155 "Waynesboro City"               
    "Valley"       2.0576131 "Charlottesville City"          
    "Valley"       2.0057306 "Harrisonburg City"             
    "Valley"       1.7856346 "Rockbridge County"             
    "Valley"        1.761106 "Greene County"                 
    "Valley"         1.58831 "Nelson County"                 
    "Valley"       1.5384616 "Staunton City"                 
    "Valley"       1.4773777 "Albemarle County"              
    "Valley"       1.2239152 "Amherst County"                
    "Valley"       1.1817338 "Campbell County"               
    "Valley"        1.179228 "Bedford County"                
    "Valley"       1.0271962 "Rockingham County"             
    "Valley"        .8410429 "Augusta County"                
    "Valley"        .8045052 "Fluvanna County"               
    "Valley"       .22624435 "Louisa County"                 
    "Valley"               0 "Highland County"               
    "Valley"               0 "Buena Vista City"              
    "Valley"               0 "Lexington City"                
    "Western"       9.002147 "Martinsville City"             
    "Western"        8.09628 "Alleghany Highlands"           
    "Western"       6.696686 "Danville City"                 
    "Western"       4.443457 "Craig County"                  
    "Western"      3.1894634 "Roanoke City"                  
    "Western"      2.0056539 "Franklin County"               
    "Western"       1.968293 "Pittsylvania County"           
    "Western"       1.345481 "Henry County"                  
    "Western"        .739645 "Floyd County"                  
    "Western"       .5602869 "Patrick County"                
    "Western"      .29436874 "Botetourt County"              
    "Western"       .2625671 "Montgomery County"             
    "Western"      .06707781 "Roanoke County"                
    "Western"              0 "Salem City"                    
    end


  • #2
    graph has a -by()- option. You have too many regions and divisions, so space is at a premium. Therefore, you will do well to lose "county" and "city" in the division names, whenever possible. The latter qualification is needed as you have cities and counties sharing the same name, e.g., Franklin City and Franklin County or Richmond City and Richmond County.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 doeregionname float pct_unfilled_by_division str30 divisionname
    "Central"      16.923077 "Charles City County"          
    "Central"       8.284023 "Sussex County"                
    "Central"       6.865224 "Petersburg City"              
    "Central"       4.995686 "Prince George County"          
    "Central"       4.868797 "Chesterfield County"          
    "Central"       4.782193 "Henrico County"                
    "Central"      4.7619047 "Surry County"                  
    "Central"      4.1588297 "Richmond City"                
    "Central"       3.225182 "Hopewell City"                
    "Central"       1.814443 "Dinwiddie County"              
    "Central"      1.7289073 "Colonial Heights City"        
    "Central"       1.298069 "Powhatan County"              
    "Central"       .8403361 "New Kent County"              
    "Central"       .6323461 "Hanover County"                
    "Central"              0 "Goochland County"              
    "Northern"      7.359307 "Manassas Park City"            
    "Northern"      6.511792 "Prince William County"        
    "Northern"      6.408716 "Madison County"                
    "Northern"      5.793743 "Manassas City"                
    "Northern"      5.521811 "Culpeper County"              
    "Northern"       4.87271 "Orange County"                
    "Northern"      4.733003 "Winchester City"              
    "Northern"      4.629316 "Shenandoah County"            
    "Northern"      3.404835 "Warren County"                
    "Northern"     3.3064265 "Fairfax County"                
    "Northern"     2.9411764 "Rappahannock County"          
    "Northern"     2.8389294 "Alexandria City"              
    "Northern"      2.597558 "Frederick County"              
    "Northern"      1.742286 "Fauquier County"              
    "Northern"      1.571277 "Page County"                  
    "Northern"      1.376601 "Arlington County"              
    "Northern"     1.1442386 "Loudoun County"                
    "Northern"      .7491198 "Clarke County"                
    "Northern"             0 "Falls Church City"            
    "Nothern Neck" 10.416667 "King and Queen County"        
    "Nothern Neck"  9.123493 "King George County"            
    "Nothern Neck"  8.976661 "Westmoreland County"          
    "Nothern Neck"  8.047891 "Fredericksburg City"          
    "Nothern Neck"  7.571521 "Spotsylvania County"          
    "Nothern Neck"  5.765766 "Caroline County"              
    "Nothern Neck"  5.222614 "Lancaster County"              
    "Nothern Neck" 4.2016807 "Colonial Beach"                
    "Nothern Neck" 3.5137036 "Northumberland County"        
    "Nothern Neck"  3.211304 "Richmond County"              
    "Nothern Neck" 2.7881715 "Stafford County"              
    "Nothern Neck"  2.728699 "King William County"          
    "Nothern Neck"  2.302935 "Gloucester County"            
    "Nothern Neck" 1.9602078 "Middlesex County"              
    "Nothern Neck" 1.2554928 "Mathews County"                
    "Nothern Neck" 1.0072522 "Essex County"                  
    "Nothern Neck"         0 "West Point"                    
    "Southside"    14.092053 "Nottoway County"              
    "Southside"     10.48951 "Prince Edward County"          
    "Southside"     6.934673 "Brunswick County"              
    "Southside"     6.001372 "Lunenburg County"              
    "Southside"     5.665169 "Cumberland County"            
    "Southside"    4.5045047 "Buckingham County"            
    "Southside"     4.316547 "Mecklenburg County"            
    "Southside"     3.418601 "Halifax County"                
    "Southside"    1.7847582 "Amelia County"                
    "Southside"     1.174743 "Appomattox County"            
    "Southside"     .5947778 "Greensville County"            
    "Southside"            0 "Charlotte County"              
    "Southwest"    10.600707 "Bland County"                  
    "Southwest"     3.733665 "Lee County"                    
    "Southwest"     3.571842 "Dickenson County"              
    "Southwest"    3.3163266 "Tazewell County"              
    "Southwest"     2.887956 "Giles County"                  
    "Southwest"     1.690903 "Grayson County"                
    "Southwest"    1.5967555 "Smyth County"                  
    "Southwest"    1.5154576 "Buchanan County"              
    "Southwest"    1.2872498 "Pulaski County"                
    "Southwest"    1.2543117 "Wise County"                  
    "Southwest"    1.1656694 "Galax City"                    
    "Southwest"    1.0759629 "Carroll County"                
    "Southwest"     1.039249 "Washington County"            
    "Southwest"      .739027 "Wythe County"                  
    "Southwest"     .4830918 "Bristol City"                  
    "Southwest"     .3472584 "Russell County"                
    "Southwest"            0 "Scott County"                  
    "Southwest"            0 "Radford City"                  
    "Southwest"            0 "Norton City"                  
    "Tidewater"    20.830753 "Southampton County"            
    "Tidewater"    16.475494 "Portsmouth City"              
    "Tidewater"    15.176023 "Norfolk City"                  
    "Tidewater"    12.953368 "Franklin City"                
    "Tidewater"     9.324963 "Newport News City"            
    "Tidewater"     8.720815 "Suffolk City"                  
    "Tidewater"     6.656608 "Northampton County"            
    "Tidewater"     5.902958 "Poquoson City"                
    "Tidewater"     5.678463 "Hampton City"                  
    "Tidewater"    3.0737975 "Accomack County"              
    "Tidewater"     2.795049 "Chesapeake City"              
    "Tidewater"     2.282772 "Virginia Beach City"          
    "Tidewater"    2.1390374 "York County"                  
    "Tidewater"    1.7799023 "Isle of Wight County"          
    "Tidewater"    1.1473153 "Williamsburg-James City County"
    "Valley"       10.538675 "Lynchburg City"                
    "Valley"       4.5545635 "Bath County"                  
    "Valley"       2.0973155 "Waynesboro City"              
    "Valley"       2.0576131 "Charlottesville City"          
    "Valley"       2.0057306 "Harrisonburg City"            
    "Valley"       1.7856346 "Rockbridge County"            
    "Valley"        1.761106 "Greene County"                
    "Valley"         1.58831 "Nelson County"                
    "Valley"       1.5384616 "Staunton City"                
    "Valley"       1.4773777 "Albemarle County"              
    "Valley"       1.2239152 "Amherst County"                
    "Valley"       1.1817338 "Campbell County"              
    "Valley"        1.179228 "Bedford County"                
    "Valley"       1.0271962 "Rockingham County"            
    "Valley"        .8410429 "Augusta County"                
    "Valley"        .8045052 "Fluvanna County"              
    "Valley"       .22624435 "Louisa County"                
    "Valley"               0 "Highland County"              
    "Valley"               0 "Buena Vista City"              
    "Valley"               0 "Lexington City"                
    "Western"       9.002147 "Martinsville City"            
    "Western"        8.09628 "Alleghany Highlands"          
    "Western"       6.696686 "Danville City"                
    "Western"       4.443457 "Craig County"                  
    "Western"      3.1894634 "Roanoke City"                  
    "Western"      2.0056539 "Franklin County"              
    "Western"       1.968293 "Pittsylvania County"          
    "Western"       1.345481 "Henry County"                  
    "Western"        .739645 "Floyd County"                  
    "Western"       .5602869 "Patrick County"                
    "Western"      .29436874 "Botetourt County"              
    "Western"       .2625671 "Montgomery County"            
    "Western"      .06707781 "Roanoke County"                
    "Western"              0 "Salem City"                    
    end
    
    gen name2= ustrregexra(lower(divisionname), "\bcity\b|\bcounty\b", "")
    replace name2= name2+ " cty" if inlist( lower(divisionname) , "franklin city", "richmond city", "roanoke city")
    set scheme s1mono
    graph hbar (asis) pct_unfilled_by_division, over(name2, sort(1) descending lab(labsize(tiny))) by(doeregionname, note("")) nofill
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	103.5 KB
ID:	1711382

    Last edited by Andrew Musau; 26 Apr 2023, 15:22.

    Comment


    • #3
      [@Andrew Musau got further.]

      Note: Northern Neck.
      Last edited by Nick Cox; 26 Apr 2023, 15:37.

      Comment


      • #4
        We can free up more space by eliminating the plot region margin.

        Code:
        graph hbar (asis) pct_unfilled_by_division, over(name2, sort(1) ///
        descending lab(labsize(vsmall))) by(doeregionname, yrescale note("")) ///
        nofill plotregion(margin(zero))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	126.2 KB
ID:	1711385

        Comment


        • #5
          I would go with Nick's code and eliminating the margin + resizing the labels.

          Code:
          graph hbar (asis) pct, over(division, sort(1) descending lab(labsize(2))) by(doe, compact note("") col(2)) nofill ysize(7) plotregion(margin(zero))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	103.9 KB
ID:	1711388

          Last edited by Andrew Musau; 26 Apr 2023, 15:50.

          Comment


          • #6
            Beautiful! This is what I wanted. Thank you very much, Andrew and Nick!

            Comment

            Working...
            X