Showing posts with label graph dot. Show all posts
Showing posts with label graph dot. Show all posts

Apr 20, 2015

Random graphs (45): Re-creating -graph dot- using -twoway dot-



This plot recreates this one, but adds confidence intervals.
preserve

// Calculate country-specific means to be plotted
tempname foo
tempname idealage
postfile `foo' quintile incocomp incocomp_lb incocomp_ub using `foo2', replace

levelsof(quintile), local(quintile)        
        
foreach x of local quintile {
 qui reg incocomp if quintile == `x', cluster(cntry)
 local incocomp   = _b[_cons]
 local incocomplb = _b[_cons] - (1.96 * _se[_cons])
 local incocompub = _b[_cons] + (1.96 * _se[_cons])
 post `foo' (`x') (`incocomp') (`incocomplb') (`incocompub')
}

postclose `foo'

// Plot country-specific means
use `foo2', clear
    
twoway (dot incocomp quintile) ///  -twoway scatter- doesn't have the lines
       (rcap incocomp_lb incocomp_ub quintile) ///
      , yscale(range(1.5 3.0)) ///
        ylabel(1.5 (.25) 3.0, format(%6.2f)) ///
 legend(off)
        title("Income quintiles")
        xlabel(1 `""1" "(poorest)""' /// Double-compound quotes allow for the line break
               2 3 4 5 `""5" "(richest)"') ///
        fxsize(40) /// Forces x-size to be 40 percent of its original size
        xtitle("") ///
        xscale(range(0.2 5.8)) /// Making this a bit bigger makes room for the labels
 name(byquintile_hor, replace) 

restore

// Calculate country-specific means to be plotted 2
preserve
tempname foo
tempname idealage
postfile `foo' str2 cntry incocomp incocomp_lb incocomp_ub using `foo2', replace


levelsof(cntry), local(cntry)        
        
foreach x of local cntry {
 qui reg incocomp if cntry == "`x'"
 local incocomp   = _b[_cons]
 local incocomplb = _b[_cons] - (1.96 * _se[_cons])
 local incocompub = _b[_cons] + (1.96 * _se[_cons])
 post `foo' ("`x'") (`incocomp') (`incocomplb') (`incocompub')
}

postclose `foo'

use `foo2', clear

// Create sorted-by-size variable
egen order_ = rank(-incocomp), unique
labmask order_, value(cntry)

// Plot country-specific means 2      
twoway (dot incocomp order_) ///
       (rcap incocomp_lb incocomp_ub order_) ///
      , yscale(off) /// Not necessary, as we're using the one from the other graph
        xlabel(1/23, val alt) ///
        title("Countries") ///
        xtitle("") ///
        legend(off) ///
        /* graphregion(margin(b=16)) */ /// Could help as -ycommon- isn't accounting for the different label sizes
        name(bycntry_hor, replace)

// Combine plots
graph combine byquintile_hor bycntry_hor, ///
      l1title("Mean income comparison orientation") ///
      ycommon row(1) ///
      imargin(zero) // Sets margin between both plots to 0

restore

Oct 18, 2013

Random graphs (17): Differences in percentages across groups

clear

// Read in data

/// Employment (main characteristics and rates) - annual averages [lfsi_emp_a]
/// Last update: 10-07-2013 
/// Employment rate (20 to 64 years) 

input str33 geo total male female
"European Union (28 countries)" 68.4 74.5 62.3
"European Union (27 countries)" 68.5 74.6 62.4
"Belgium" 67.2 72.7 61.7
"Bulgaria" 63.0 65.8 60.2
"Czech Republic" 71.5 80.2 62.5
"Denmark" 75.4 78.6 72.2
"Germany" 76.7 81.8 71.5
"Estonia" 72.1 75.2 69.3
"Ireland" 63.7 68.1 59.4
"Greece" 55.3 65.3 45.2
"Spain" 59.3 64.5 54.0
"France" 69.3 73.8 65.0
"Croatia" 55.4 60.6 50.2
"Italy" 61.0 71.6 50.5
"Cyprus" 70.2 76.1 64.8
"Latvia" 68.2 70.2 66.4
"Lithuania" 68.7 69.4 67.9
"Luxembourg" 71.4 78.5 64.1
"Hungary" 62.1 68.1 56.4
"Malta" 63.1 79.0 46.8
"Netherlands" 77.2 82.5 71.9
"Austria" 75.6 80.9 70.3
"Poland" 64.7 72.0 57.5
"Portugal" 66.5 69.9 63.1
"Romania" 63.8 71.4 56.3
"Slovenia" 68.3 71.8 64.6
"Slovakia" 65.1 72.8 57.3
"Finland" 74.0 75.5 72.5
"Sweden" 79.4 81.9 76.8
"United Kingdom" 74.2 80.0 68.4
"Iceland" 81.8 84.4 79.1
"Norway" 79.9 82.4 77.3
"Switzerland" 82.0 87.9 76.0
end

/// Create country variable
kountry geo, from(other) stuck
ren _ISO3N_ ctry
kountry ctry, from(iso3n) to(iso2c)
replace _ISO2C_ = "EU-27" if geo == "European Union (27 countries)"
replace _ISO2C_ = "EU-28" if geo == "European Union (28 countries)"
replace _ISO2C_ = "GB" if geo == "United Kingdom"
replace _ISO2C_ = "EL" if geo == "Greece"
drop ctry
ren _ISO2C_ cntry

drop if inlist(cntry, "EU-28", "IS", "CH", "NO")

graph dot male female, over(cntry, sort(total) ///
      label(angle(v))) vertical exclude0 ///
   legend(label(1 "Males") label(2 "Females") ring(0)) ///
   ytitle("Employment rate among 20-64 year-olds," "2012") ///
   yline(75) text(76 12 "Europe 2020 target") ///
   note( ///
          "{it:Source:} Eurostat, lfsi_emp_a, date of extraction: 2013-08-04" ///
          "{it:Note:} Countries sorted by total employment rates", span)

Apr 11, 2013

Random graphs (14): Combining dot plots

collapse (mean) incocomp, by(quintile)

graph dot incocomp, ///
      over(quintile, relabel(1 `""1" "(poorest)""'  5 `""5" "(richest)""')) ///
      vertical /// // Labels with line break
      ytitle("Income comparison orientation") ///
      yscale(range(1.5 3.0)) ///
      ylabel(1.5 (.25) 3.0, format(%6.2f)) /// // Format axis numbering
      exclude0 ///
      b1title("Income quintile averages") /// // Label over() axis
      name(byquintile, replace) ///
      fxsize(50) // Reduce size for combining

restore 
collapse (mean) incocomp, by(cntry)

// Create neatly ordered variable for x-axis
egen order = rank(-incocomp), unique
encode cntry, gen(geo)
labmask order, value(geo) decode

graph dot incocomp, over(order, label(alternate)) vertical ///
          ytitle("Income comparison orientation") ///
          name(bycountry, replace) ///
          ylabel(1.5 (.25) 3.0, format(%6.2f)) exclude0 yscale(off) ///
   b1title("Country averages") fxsize(100)

graph combine byquintile bycountry, xsize(6) ycommon imargin(zero)


restore
collapse (mean) incocomp, by(quintile cntry)

// Get data into proper shape
sort cntry quintile
reshape wide incocomp, i(cntry) j(quintile)

// Create ordered variable for x-axis labels
gen diff = incocomp5 - incocomp1 // Income comparison gap
egen order = rank(incocomp5), unique
encode cntry, gen(geo)
labmask order, value(geo) decode
sort order

// Create variables as marker labels
gen incocomp1l = 1
gen incocomp3l = 3
gen incocomp5l = 5

twoway (pcspike incocomp1 order incocomp5 order, lcolor(gs14)) ///
       (scatter incocomp1 order, mlabel(incocomp1l) mlabpos(0) msymbol(none)) ///
       (scatter incocomp5 order, mlabel(incocomp5l) mlabpos(0) msymbol(none)) ///
        , xlab(1/23, valuelabels ang(v)) ///
   ytitle("Average income comparison orientation") ///
   ylabel(, format(%6.1f)) ///
   xtitle("") ///
   name(bycountry, replace) ///
   legend(label(1 "Test") ///
          label(2 "1 First quintile (poorest)") ///
   label(3 "5 Fifth quintile (richest)") ///
   order(2 3) ring(0) pos(5))

Mar 15, 2013

Random graphs (7): Differences in percentages across groups

*findit labutil // For the -labmask- command
*ssc install kountry

clear 
version 12 
  
// Download date from Eurostat, lfsa_eppga 
// Part-time employment as percentage of the total employment 
// From 15 to 64 years

input str60 geo m2011 f2011
EU27 8.1 31.6
Belgium 9.2 43.3
Bulgaria 2.0 2.4
"Czech Republic" 1.8 8.5
Denmark 14.2 37.0
Germany 9.0 45.1
Estonia 5.0 13.5
Ireland 12.5 35.2
Greece 4.2 10.0
Spain 5.9 23.4
France 6.5 29.9
Italy 5.5 29.3
Cyprus 6.1 12.1
Latvia 7.0 10.4
Lithuania 6.6 9.9
Luxembourg 4.3 35.9
Hungary 4.4 8.8
Malta 5.3 25.6
Netherlands 24.3 76.5
Austria 7.8 43.4
Poland 4.7 10.4
Portugal 7.0 13.7
Romania 8.7 10.1
Slovenia 7.1 12.2
Slovakia 2.6 5.6
Finland 9.4 19.0
Sweden 12.3 39.3
"United Kingdom" 11.0 42.2
Iceland 9.9 31.7
Norway 13.7 42.1
Switzerland 12.4 59.4
end 
 
// Country 
kountry geo, from(other) stuck
ren _ISO3N_ ctry
kountry ctry, from(iso3n) to(iso2c)
kountryadd "Former Yugoslav Republic of Macedonia, the" to "Macedonia" add
replace _ISO2C_ = "EU-27" if geo == "EU27" 
drop ctry
ren _ISO2C_ cntry 
  // Create non-string version of country variable
encode cntry, gen(country)
 
// Drop unnecessary cases
drop if cntry == "MK"
drop if cntry == "HR" 
drop if cntry == "TR" 

// First attempts:
graph bar f2011 m2011, over(cntry, sort(1) desc label(angle(90))) ///
      legend(label(2 "Men") label(1 "Women")) ///
   xsize(8.25) ysize(4)
// A bar graph is too cluttered

graph dot f2011 m2011, over(cntry, sort(1) desc) ///
      legend(label(2 "Men") label(1 "Women")) ///
   xsize(8.25) ysize(4)
// Can I have that vertically as well?

twoway dot m2011 f2011 country, vertical ///
      legend(label(1 "Men") label(2 "Women") ring(0) pos(1)) ///
   xlabel(1/31, valuelabels ang(v)) ///    // turn on labels
   xtitle("") ///
   ytitle("% part-time of total employment in 2011") ///
   caption("Source: Eurostat, lfsa_eppga, 2013-03-15", span) ///
   xsize(8.25) ysize(4)
// Can I sort countries by size of the gender gap?

// Set up sort order
gen diff = f2011 - m2011  // Gender gap
egen order = rank(-diff), unique  // Create rank variable
labmask order, value(country) decode // Assign value
  // labels of country to variable order based on its values
   
// Create graph
twoway dot m2011 f2011 order, vertical ///
      legend(label(1 "Men") label(2 "Women") ring(0) pos(1)) ///
   xlabel(1/31, valuelabels ang(v)) ///    // turn on labels
   xtitle("") ///
   ytitle("% part-time of total employment in 2011") ///
   caption("Source: Eurostat, lfsa_eppga, 2013-03-15", span) ///
   xsize(8.25) ysize(4)