Showing posts with label twoway connected. Show all posts
Showing posts with label twoway connected. Show all posts

Mar 23, 2018

Random graphs (129): Offsetting markers

// Open Allbus 2016
use eastwest hs01 id02 using ZA5250_v2-0-0.dta, clear

// Region
recode eastwest (1 = 0 "West Germany") (2 = 1 "East Germany"), gen(east)
label var east "East Germany"

// Subjective social class
recode id02 (-50/-7 = .) ///
            (     1 = 0 "Lower class") ///
            (     2 = 1 "Working class") ///
            (     3 = 2 "Middle class") ///
            (   4 5 = 3 `" "Upper" "(middle)" "class" "'), gen(class)
label var class "Subjective social class"

// Health
recode hs01 (-9 = .) (5 = 0 "Bad") (4 = 1 "Less good") (3 = 2 "Satisfactory") ///
            (2 = 3 "Good") (1 = 4 "Very good"), gen(health)
label var health "Self-rated health"

// Model
regress health i.class##i.east //[pw = wghtpew]

// Plot using marginsplot
margins class#east, saving(myfile, replace) 
 
marginsplot, legend(pos(4) ring(0)) name(marginsplot, replace) title("") ///
             ytitle("Predicted self-rated health") ylabel(, format(%6.1f))

// Plot using twoway with overlay
use myfile, clear

// Create offset
clonevar _mx = _m1
replace _mx = cond(_m2 == 2, _mx - 0.1, _mx + 0.1)

// Plot using marginsplot
 
twoway (rcap _ci_lb _ci_ub _m1 if _m2==0, sort) ///
       (rcap _ci_lb _ci_ub _mx if _m2==1, sort) ///
       (connected _margin _m1 if _m2==0, msymbol(oh)) ///
       (connected _margin _mx if _m2==1, msymbol(o)) ///
       , title("") ///
        ytitle("Predicted self-rated health") xla(, valuelabel) ///
  legend(pos(4) ring(0) col(1) order(3 "West Germany" 4 "East Germany")) ///
  name(twoway, replace) ylabel(, format(%6.1f))

Jan 7, 2018

Random graphs (124): Line plots

 
clear
input str100 grouping t1 t2
"Department managers" .22 .399
"Less than year" .35 .21
"Remaining non-managers" .31 .36
end

reshape long t, i(grouping) j(treatment)

twoway (connect t treatment if grouping == "Department managers", msymbol(o)) ///
       (connect t treatment if grouping == "Less than year", msymbol(o)) ///
       (connect t treatment if grouping == "Remaining non-managers", msymbol(o)) ///
       (scatteri .399 2 "Department managers" ///
                 .21  2 "Less than year" ///
                 .36  2 "Remaining non-managers", msymbol(none)) ///
     , legend(off) xtitle("Minimum wage raise") xlabel(0 " " 1 "Before" 2 "After" 3 " ", notick)  ///
       ytitle("Job satisfaction") ylabel(, format(%6.2f))

Dec 2, 2017

Random graphs (120): Uncluttered line plots

// Use OECD data
sdmxuse data OECD, dataset(IDD) clear attributes

// Generate country variable
kountry location, from(iso3c)
rename NAMES_STD country

// Generate year variable
destring time, gen(year)

// Keep relevant data
keep if measure == "GINI"
keep if age == "TOT"
keep if definition == "CURRENT"
keep if inlist(country, "Netherlands", "Germany", "United Kingdom", "United States")
drop if methodo == "METH2012"

sort country year
twoway (connected value year if country == "Germany") ///
       (line value year if country == "Netherlands", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "United Kingdom", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "United States", lcolor(gs10) lpattern(solid)) ///
      , legend(off) name(pa, replace) title(Germany) xtitle("") ytitle("Income inequality") ///
        ylabel(, format(%6.2f)) 
twoway (connected value year if country == "Netherlands") ///
       (line value year if country == "Germany", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "United Kingdom", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "United States", lcolor(gs10) lpattern(solid)) ///
      , legend(off) name(pb, replace) title(Netherlands) xtitle("") ytitle("Income inequality") ///
        ylabel(, format(%6.2f)) 
twoway (connected value year if country == "United Kingdom") ///
       (line value year if country == "Germany", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "Netherlands", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "United States", lcolor(gs10) lpattern(solid)) ///
      , legend(off) name(pc, replace) title(United Kingdom) xtitle("") ytitle("Income inequality") ///
        ylabel(, format(%6.2f)) 
twoway (connected value year if country == "United States") ///
       (line value year if country == "Germany", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "Netherlands", lcolor(gs10) lpattern(solid)) ///
       (line value year if country == "United Kingdom", lcolor(gs10) lpattern(solid)) ///
      , legend(off) name(pd, replace) title(United States) xtitle("") ytitle("Income inequality") ///
        ylabel(, format(%6.2f))
    
graph combine pa pb pc pd, col(2) name(fig1, replace)

Sep 7, 2017

Random graphs (112): Line plot

clear 
// Open Eurostat data
unzipfile "macrodata\lfsi_pt_a.zip"
insheet using lfsi_pt_a_1_Data.csv

// Prepare variables
replace value = "." if value == ":"
destring value, gen(fixedterm)
rename time year
replace geo = "France" if geo == "France (metropolitan)"
kountry geo, from(other) stuck
rename _ISO3N_ country
kountry country, from(iso3n) to(iso2c)
rename _ISO2C_ cntry

// Select data and save
keep if year >= 2004
keep fixedterm geo year cntry
save lfs_fixedterm, replace

// Open ESS data
use essround agea mnact wrkctra pspwght cntry using ESS1-7e01, clear

// Select data
keep if essround >= 2
keep if inrange(agea, 20, 64)
keep if mnact == 1

// Prepare variables
generate ess_fixedterm = (wrkctra == 2)
replace  ess_fixedterm = (ess_fixedterm * 100)
generate year = 2004 if essround == 2
replace  year = 2006 if essround == 3
replace  year = 2008 if essround == 4
replace  year = 2010 if essround == 5
replace  year = 2012 if essround == 6
replace  year = 2014 if essround == 7

// Point estimates and standard errors
statsby ess_fixedterm = _b[_cons] se = _se[_cons], clear by(cntry year): regress ess_fixedterm

// Merge Eurostat data with ESS data
merge 1:1 cntry year using lfs_fixedterm

// Select countries 
drop if inlist(cntry, "RU", "IL", "UA", "LV", "MK", "MT", "RO")

// Generate country name variable
kountry cntry, from(iso2c) 
ren NAMES_STD country

// Calculate confidence intervals
generate lb = ess_fixedterm - 1.96 * se
generate ub = ess_fixedterm + 1.96 * se

// Plot figure
sort year
twoway (rarea ub lb year, lcolor(white)) ///
       (connected ess_fixedterm year) ///
       (line fixedterm year), by(country, note("")) ///
        xtitle("") ytitle("Percentage of total employed (20{c 150}64 y)" "on temporary contract") ///
        legend(order(2 "ESS" 1 "95% CI" 3 "Eurostat") row(1)) xlabel(2004 (2) 2014)


May 4, 2016

Random graphs (80): Line plots using the -by- option

unzipfile output961465243413697143.zip, replace

use ESS1-6e01_1_F1.dta, clear

// Label waves
label define essround 1 "2002" 2 "2004" 3 "2006" 4 "2008" 5 "2010" 6 "2012"
label val essround essround 

// Keep only countries that contribute six observations
egen pickone = tag(cntry essround)
tab cntry essround if pickone // Look at no. of waves per country
egen numrounds = total(pickone), by(cntry) // Calculate no. of waves per country
keep if numrounds == 6

// Identify migrants
generate migrant = 1 if (brncntr == 2) & !missing(brncntr)
replace  migrant = 2 if (brncntr == 1 & (facntr == 2 | mocntr == 2)) & !missing(brncntr, facntr, mocntr)
replace  migrant = 0 if (brncntr == 1 &  facntr == 1 & mocntr == 1) & !missing(brncntr, facntr, mocntr)

label define migrant 0 "Native" 1 "First-generation migrant" 2 "Second-generation migrant"
label value migrant migrant

// Generate country identifier
kountry cntry, from(iso2c)
rename NAMES_STD country
replace country = "Slovenia" if country == "si"

// By country, year, and migrant status
collapse (mean) imbgeco imueclt imwbcnt [pweight = dweight], by(country essround migrant)

drop if migrant == .

sort country migrant essround
twoway (connected imbgeco essround if migrant == 0, ///
         by(country, note(" " "{it:Source:} European Social Survey, rounds 1{c 150}6, weighted data", span size(*.8)))) ///
       (connected imbgeco essround if migrant == 1) ///
       (connected imbgeco essround if migrant == 2) ///
      , xlabel(1/6, val ang(v)) ytitle("Immigration good for economy") ysize(8) xtitle("") name(imbgecocym, replace) ///
        legend(order(1 "Natives" ///
                     2 "First-gen." ///
                     3 "Second-gen.") ring(0) row(1)) 

Apr 26, 2016

Random graphs (75): Line plots

use essround cntry netuse dweight using ESS1-6e01_0_F1.dta, clear

// Drop parts of data and label wave identifier
drop if essround == 6 // Doesn't have internet use variable
drop if inlist(cntry, "IS", "LT") // drop countries with only one measurement
label define essround 1 "2002" 2 "2004" 3 "2006" 4 "2008" 5 "2010"
label val essround essround 

// Generate variable of interest
generate dailyuser = (netuse == 7) if !missing(netuse)

// Generate values by country and round
collapse (mean) dailyuser [pweight = dweight], by(cntry essround)
replace dailyuser = dailyuser * 100

// Generate country identifier
kountry cntry, from(iso2c)
rename NAMES_STD country

// Plot
twoway (connected dailyuser essround, ///
        by(country, note(" " "{it:Source:} European Social Survey, rounds 1{c 150}5, weighted data", span size(*.8)) ///
     col(3))), xlabel(, val) ytitle("% daily internet users") ysize(8) xtitle("")

Nov 7, 2014

Random graphs (36): Small-multiples line plot

clear
input str2 cntry r1998 r2001 r2004 r2007 r2010 r2013
AT 6 6 6 6 6 3
BE 6 6 2 3 6 6
BG . . 4 6 6 3
CA 6 6 6 6 . .
CH . 4 3 6 3 3
CZ 4 4 6 3 . 6
DE 3 3 3 3 3 .
DK 3 3 3 3 2 3
GR 6 6 6 4 4 .
ES 6 6 6 3 3 3
FI 6 6 6 6 . 6
FR 6 6 6 . . 3
HR . . . 3 3 .
HU 4 4 4 4 4 4
IE 6 3 6 6 . 6
IL 6 4 6 6 6 1
IS . . . . 2 6
IT 4 4 6 3 . 6
JP 4 4 3 . 1 2
LV . . . 3 3 3
NL 6 6 6 6 2 .
NO 6 6 6 3 . 6
PL 3 3 3 . 6 .
PT 6 3 4 6 . .
RO . . 6 6 3 .
SE 3 3 2 2 2 2
SI . . 3 3 3 3
TR 6 6 6 6 3 2
UK 2 2 3 2 3 2
US 6 5 6 5 . 6
end

reshape long r, i(cntry) j(year)

label define r 6 "No limit"
label val r r

twoway (connected r year, by(cntry, note("")) /// // by(, note("")) suppresses
                          ylabel(0(1)6, val) ///  // "Graphs by"
                          xlabel(1998 2001 2004 2007 2010 2013) ///                          
                          lwidth(thick) cmissing(no) msize(large) msymbol(O) ///
                          xtitle("") ytitle("Maximum number of embryos per cycle"))