use "ESS3e03_5.dta", clear
// Drop two remote countries
drop if cntry == "UA"
drop if cntry == "RU"
// Prepare variables of interest
// Country variable
encode(cntry), gen(country)
label define country 12 "UK", modify
// Keep only respondents asked about women (split ballot)
keep if icsbfm == 1
/// Question about values
recode aftjbyc (1 2 = 1 "(Strongly) disapprove") ///
(3 = 2 "Neutral") ///
(4 5 = 3 "(Strongly) approve") ///
(7/9 = .a "Missing/NA") ///
, gen(value) label(value)
quietly tab value, gen(val)
ren val1 disapprove
ren val2 neutral
ren val3 approve
// Sex
generate female = (gndr == 2) if gndr != .a
drop if female == .
// Calculate average scores across all countries by gender
preserve
collapse (mean) disapprove neutral approve [pweight = dweight], by(female)
gen country = 50 // Assign some value to sample average
tempfile euaverage
save `euaverage', replace
restore
// Calculate average scores for each country by gender
collapse (mean) disapprove neutral approve [pweight = dweight], by(country female)
// Add average score
append using `euaverage'
label define country 50 "{bf:EU}", modify // Add bold label for sample averages
// Plot
graph bar approve neutral disapprove if female == 1 ///
, over(country, sort(approve) descending label(alternate)) stack percentages ///
title("Popular (dis)approval of a full-time" "working woman with a child under 3 years of age") ///
ytitle("% of women") yscale(range(0 100)) ylabel(0(20)100) legend(off) ///
name(women, replace)
graph bar approve neutral disapprove if female == 0 ///
, over(country, sort(approve) descending label(alternate)) stack percentages ///
ytitle("% of men") yscale(range(0 100)) ylabel(0(20)100) ///
legend(label(3 "(Strongly) disapprove") ///
label(2 "Neutral") ///
label(1 "(Strongly) approve") ///
order(1 2 3) pos(6) row(1)) ///
caption(" " ///
"{it: Source:} European Social Survey 2006/07, own calculations." ///
`"{it: Note:} "EU" refers to average for the 20 EU member states in this Figure."', ///
span size(small)) ///
name(men, replace)
graph combine women men, col(1) ysize(8)
Showing posts with label tempfile. Show all posts
Showing posts with label tempfile. Show all posts
Jan 8, 2015
Random graphs (42): Stacked bar graph
Dec 18, 2014
Random graphs (40): Line plot
clear
// Read in Table
input str4 cntry P1970 P1991 P1998 P2008 S1970 S1991 S1998 S2008 T1970 T1991 T1998 T2008
EU15 16.2 7.6 4.5 3.4 37.4 28.6 26.6 23.2 54 73 68.9 73.5
DE 8 3.4 2.5 2.1 48 35 30.4 25.3 44 62 67.1 72.5
FI 20 8 6.3 4.8 34 29 27.8 25.6 46 63 65.9 69.6
NL 6 4 3.6 3 36 24 19.9 16.7 58 72 76.5 80.3
SE 8 4 3.1 2.2 39 28 25.1 22.7 53 68 71.8 75.1
UK 3 2.1 1.9 1.5 42 25 . 17.7 55 73 75.3 80.7
CZ 13.5 10 5.6 3.5 49.4 45.9 41.4 38 37 44 53 58.6
end
// Crazy stuff to bring it into shape
preserve
drop S1970-T2008
reshape long P, i(cntry) j(year)
tempfile p
save `p', replace
restore
preserve
drop P1970-P2008 T1970-T2008
reshape long S, i(cntry) j(year)
tempfile s
save `s', replace
restore
drop P1970-S2008
reshape long T, i(cntry) j(year)
merge 1:1 cntry year using `p'
drop _merge
merge 1:1 cntry year using `s'
// Plot
twoway (scatter P year if cntry == "EU15", connect(L)) ///
(scatter S year if cntry == "EU15", connect(L)) ///
(scatter T year if cntry == "EU15", connect(L)) ///
, ytitle("Percentage of EU-15 workforce") ///
xtitle("") ///
note(" " "{it: Source:} Mau and Verwiebe 2010, pp. 153{c 150}4", span) ///
legend(label(1 "{bf:Primary sector:}" "Agriculture") ///
label(2 "{bf:Secondary sector:}" "Industry and" "construction") ///
label(3 "{bf:Tertiary sector:}" "Services")) ///
xlabel(1970 1991 1998 2008)
Labels:
input,
preserve,
Random graphs,
reshape,
tempfile,
twoway scatter
Dec 17, 2014
Random graphs (39): Plotting on a log axis
Rather than looking at a variable in absolute terms, namely GDP per capita in the upper panel of the Figure, it is also sometimes helpful to look at it in terms of percentage increases, as in the lower panel. Equal distances on the x-axis refer to equal percentage increases in GDP per capita. Four equidistant points on the x-axis are labeled, each indicating a fourfold increase in GDP.
use wvs2005_v20090901a.dta, clear
// Country variable
// 1) Turn into string
decode v2, gen(ctry)
// 2) Abbreviate
kountry ctry, from(other) stuck marker
ren _ISO3N_ cntry
kountry cntry, from(iso3n) to(iso2c)
ren _ISO2C_ country
// Generate outcome: % in good health
generate goodhealth = 100 if v11 <= 2
replace goodhealth = 0 if v11 > 2
replace goodhealth = . if v11 == .
// Collapse data set
collapse (mean) goodhealth [pw = v259], by(country)
// Generate year variable
gen year = 2006
// Preserve collapsed data set
preserve
// Get GDP from World Bank data base
wbopendata, language(en - English) country() topics() indicator(NY.GDP.PCAP.PP.CD) clear long
// Fix obtained data set
ren ny_gdp_pcap_pp_cd gdp
ren iso2code country
keep if year == 2006
keep gdp country
drop if country == ""
// Save obtained GDP data
tempfile gdp
save `gdp'
// Restore
restore
// Merge GDP with collapsed data set
merge m:1 country using `gdp'
keep if _merge == 3
drop _merge
// Create labels with thousand separator
label define gdp 20000 "20,000" ///
40000 "40,000" ///
60000 "60,000"
label val gdp gdp
// Plot on unlogged axis
twoway (scatter goodhealth gdp, mlabel(country) mlabpos(0) msymbol(none)) ///
(lfit goodhealth gdp) ///
, legend(off) xtitle("GDP per capita, 2006, PPP in current international $") ///
ytitle("% in good health") ///
xlabel(0(20000)60000, valuelabels) ///
name(unlogged, replace)
// Generate logged variable
gen loggdp = log(gdp) * 1000 // Multiply by 1,000 because only integers can be labeled
// Generate numbers for labeling
// Round them to three decimal digits, then multiply by 1,000 to get integers
local log1 = round(log(1000), .001) * 1000
local log2 = round(log(1000 * 4), .001) * 1000
local log3 = round(log(1000 * 4 * 4), .001) * 1000
local log4 = round(log(1000 * 4 * 4 * 4), .001) *1000
*di `log1' _skip(2) `log2' _skip(2) `log3' _skip(2) `log4'
// Create labels for numbers to be labeled
label define loggdp `log1' "1,000" ///
`log2' "4,000" ///
`log3' "16,000" ///
`log4' "64,000"
label value loggdp loggdp
// Plot on log axis
twoway (scatter goodhealth loggdp, mlabel(country) mlabpos(0) msymbol(none)) ///
(lfit goodhealth loggdp) ///
, legend(off) xtitle("GDP per capita, 2006, PPP in current international $") ///
ytitle("% in good health") ///
xlabel(`log1' `log2' `log3' `log4', valuelabels) ///
name(logged, replace)
// Combine plots
graph combine unlogged logged, col(1) ysize(8)
Labels:
_skip(#),
collapse,
decode,
graph combine,
kountry,
log(),
mlabel,
preserve,
Random graphs,
round(),
tempfile,
twoway lfit,
twoway scatter,
wbopendata,
World Values Survey,
ysize
Sep 14, 2014
Random graphs (29): Line plots with confidence bands
use mainstat age country refyear coeff if refyear == 2011 using eulfs.dta, replace
drop refyear // Drop unnecessary variable
generate retired = (mainstat == 4) if mainstat != . // Create dummy variable for retired
svyset [pweight = coeff] // Declare weight
// Create temporary files and postfile
tempname foo
tempname retage
postfile `foo' cntry age perc_retired perc_retired_ll perc_retired_ul using `retage' , replace
levelsof country, local(levels1)
foreach country of local levels1 {
forvalues x = 57 (5) 77 {
// Calculate proportions
capture proportion retired if age == `x' & country == `country'
*matrix list r(table)
matrix coefs = r(table)
local perc = coefs[1,2] * 100
local perc_ll = coefs[5,2] * 100
local perc_ul = coefs[6,2] * 100
qui di `country' _skip(2) `x' _skip(2) `perc_ll' _skip(2) `perc_' _skip(2) `perc_ul'
if "`perc'" != "" { // Make sure that the loop doesn't break if
post `foo' (`country') (`x') (`perc') (`perc_ll') (`perc_ul')
}
}
}
postclose `foo'
use `retage', clear
drop if perc_retired == . // Drop empty rows
// Label variables
label define age 57 "55{c 150}59 y" 62 "60{c 150}64 y" 67 "65{c 150}69 y" ///
72 "70{c 150}74 y" 77 "75{c 150}79 y", modify
label value age age
label define cntry 1 "AT" 2 "BE" 3 "BG" 4 "CH" 5 "CY" ///
6 "CZ" 7 "DE" 8 "DK" 9 "EE" 10 "ES" ///
11 "FI" 12 "FR" 13 "GR" 14 "HU" 15 "IE" ///
16 "IS" 17 "IT" 18 "LT" 19 "LU" 20 "LV" ///
21 "MT" 22 "NL" 23 "NO" 24 "PL" 25 "PT" ///
26 "RO" 27 "SE" 28 "SI" 29 "SK" 30 "UK" ///
, modify
label value cntry cntry
// Plot plots
twoway (rarea perc_retired_ll perc_retired_ul age, fcolor(gs14)) ///
(line perc_retired age) ///
, by(cntry, cols(3) legend(off) note("") ///
caption("{it:Source:} EU-LFS 2011" ///
"{it:Notes:} Gray areas denote 95 % confidence intervals"))) ///
ytitle("% retired") xtitle("Age group") ///
xlabel(57 (5) 77, val ang(v)) ///
ysize(10)
/* ,by(, note("")) suppresses the default "Graphs by" note) */
/* legend(off) needs to be in by(, legend(off)) to work */
Jul 2, 2014
Random graphs (24): Small-multiples and overlayed line plots
clear
// SG.GEN.PARL.ZS - Proportion of seats held by women in national parliaments (%)
wbopendata, indicator(SG.GEN.PARL.ZS) clear long
// Keep European countries
keep if regioncode == "ECS" // "Europe & Central Asia (all income levels)"
// Keep EU-28
keep if inlist(iso2code, "AT", "BE", "BG", "CY", "CZ", "DE", "DK", "EE", "ES") | ///
inlist(iso2code, "FI", "FR", "GB", "GR", "HR", "HU", "IE", "IT", "LI") | ///
inlist(iso2code, "LT", "LU", "LV", "NL", "PL", "PT", "RO", "SE", "SI") | ///
inlist(iso2code, "SK") // EU-28
// Identify and rename salient variables
ren sg_gen_parl_zs women
label var women "% of national parliament seats held by women"
ren iso2code cntry
drop countrycode region regioncode
// Reshape to drop empty observations
reshape wide women, i(cntry) j(year)
dropmiss women1960-women2014, force
reshape long
// Calculate EU-28 mean
preserve
collapse (mean) women, by(year)
gen cntry = "{bf:EU-28}"
tempfile eu28
save `eu28', replace
restore
append using `eu28'
// Plot small-multiples figure
twoway scatter women year ///
, by(cntry ///
, note(" ") ///
caption("{it:Source:} World Development Indicators, date of extraction: 2015-05-25", span size(small))) ///
cmissing(no) ///
connect(l) ///
msymbol(o) ///
xtitle("") xlabel(1990 1997 2000 2005 2010 2014, ang(45)) ///
ylabel(0 (10) 50) ///
name(small_multiples, replace)
// Plot overlayed figure
encode cntry, gen(country) // No strings for -xtline-
xtset country year, yearly // Declare panel
xtline women if cntry != "{bf:EU-28}" ///
, overlay ///
xlabel(1990 1997 2000 2005 2010 2014) xtitle("Year") ///
plot1opts(lpattern(dash)) plot2opts(lpattern(dash)) ///
plot3opts(lpattern(dash)) plot4opts(lpattern(dash)) ///
plot5opts(lpattern(dash)) plot6opts(lpattern(dash)) ///
plot7opts(lpattern(dash)) plot8opts(lpattern(dash)) ///
plot9opts(lpattern(dash)) plot10opts(lpattern(dash)) ///
plot11opts(lpattern(dash)) plot12opts(lpattern(dash)) ///
plot13opts(lpattern(dash)) plot14opts(lpattern(dash)) ///
plot15opts(lpattern(dash)) plot16opts(lpattern(dash)) ///
plot17opts(lpattern(dash)) plot18opts(lpattern(dash)) ///
plot19opts(lpattern(dash)) plot20opts(lpattern(dash)) ///
plot21opts(lpattern(dash)) plot22opts(lpattern(dash)) ///
plot23opts(lpattern(dash)) plot24opts(lpattern(dash)) ///
plot25opts(lpattern(dash)) plot26opts(lpattern(dash)) ///
plot27opts(lpattern(dash)) plot28opts(lpattern(dash)) ///
name(overlay, replace) ///
addplot(line women year if cntry == "{bf:EU-28}", ///
lwidth(thick) lpattern(solid) sort(cntry year)) ///
legend(order(29 "EU-28 (unweighted average)") pos(6) ring(0)) ///
caption("{it:Source:} World Development Indicators, date of extraction: 2015-05-25", span size(small)) ///
ytitle("% of national parliament seats" "held by women, EU-28 countries")
Labels:
append,
collapse,
dropmiss,
Random graphs,
reshape,
tempfile,
twoway scatter,
wbopendata,
xtline,
xtset
Subscribe to:
Posts (Atom)





