Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box plot with averages and overlapping histograms #48

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
* Figure: 10-25-50-75-90 Percentile box plot with averages

* Load data
cd "{directory}"
use "data.dta" , clear

* Generate average, median, and percentiles
egen average = mean(yield) , by(crop)
format average %9.3f
egen median = median(yield) , by(crop)

foreach percentile in 10 25 75 90 {

egen p`percentile' = pctile(yield) , by(crop) p(`percentile')
}

* Set order of bars
recode crop (1 = 1) (10 = 2) (24 = 3) (25 = 4), gen(order)

* Set graph options
local fillOptions "fcolor(eltblue) fintensity(inten50)"
local lineOptions "lcolor(eltblue) lwidth(0.275)"

* Plot by crop
#d ;

// Boxes
tw (rbar median p25 order, horiz `fillOptions' `lineOptions' barw(0.5) )
(rbar median p75 order, horiz `fillOptions' `lineOptions' barw(0.5) )

// Whiskers
(rspike p10 p25 order, horiz `lineOptions' )
(rspike p75 p90 order, horiz `lineOptions' )

(rcap p10 p10 order, horiz `lineOptions' msize(*4) )
(rcap p90 p90 order, horiz `lineOptions' msize(*4) )

// Average dots
(dot average order, horiz ndot(0)
msymbol(cirlce) msize(medlarge)
mcolor(midblue)
mlab(average) mlabpos(12) mlabgap(5)
mlabcolor(black*0.8) mlabsize(medsmall) )
,

xlab(0(0.5)2)
ylab(1 "Maize"
2 "Pigeon pea"
3 "Sesame"
4 "Soy"
, noticks angle(h) labgap(*3.5)
)

legend(order(1 "Q{sub:25}-Q{sub:75}"
5 "Q{sub:10}-Q{sub:90}"
7 "Average")
rows(1)
)

xtitle("Ton/Ha") xscale(titlegap(2))
ytitle("")
graphregion(color(white))
;
#d cr

gr export "figure.png", width(5000) replace

* End of the do-file
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions Library/Density plots/Overlapping histograms/do.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
* Figure: Overlapping histograms

* Load data
cd "{directory}"
use "data.dta" , clear
append using "data2.dta" , gen(group)

* Set general options
local histOptions "start(0) width(20) percent gap(5)"

* Plot two histograms
#d ;
tw (hist areacult_share if areacult_share > 0 & !group , `histOptions' color(navy%75) )
(hist areacult_share if areacult_share > 0 & group , `histOptions' fcolor(white%0) lcolor(eltblue) lwidth(vthick) )
,
ylab(, angle(horizontal) nogrid)
xlab(10 "0-20"
30 "20-40"
50 "40-60"
70 "60-80"
90 "80-100")
xtitle("Share of area cultivated with improved seeds (%)")

legend(order(1 "SECFs"
2 "SFs"))

xscale(titlegap(2))

plotregion(margin(l-10))
graphregion(margin(r+4))
graphregion(color(white))
;
#d cr

gr export "figure.png", width(5000) replace

* End of the do-file
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.