---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
How to build double histograms using the pyramid command:
pyramid(d1,d2, co1,co2, CL,S)
d1 and d2 are the data collections, co1 and co2 are the colors, CL are the names of the
classes, S is the string we want use as space to the right of names. If co1 and co2 are
# 0 the rectangles are empty, if they are 1 shading lines are drawn. If CL and S are
# NULL or "" no name is written.

A first example: the heigths of a group of students (males and females):



hf=c(1,8,6,7,9,4,0,0,0); hm=c(0,0,0,4,9,13,17,13,1)
BF=4.6; HF=2.5
pyramid(hf,hm, 0,0, "","")                      #  A
pyramid(hf,hm, 1,1, "","")                      #  B
pyramid(hf,hm, "pink","cyan", "","")            #  C
#
CL = c("[150,155)","[155,160)","[160,165)","[165,170)","[170,175)","[175,180)",
       "[180,185)","[185,190)","[190,195)")
pyramid(hf,hm,"pink","cyan",CL,"   ")
coldash="brown"; gridV(c(-max(hf),max(hm)))     #  D
#
pyramid(hf,hm,"pink","cyan",CL,"   ")
coldash="brown"; gridV(seq(-9,18,1))
abovex("Heigths of a group of students - F/M")  #  E
#
pyramid(hf,hm,1,1,CL,"   ")
coldash="brown"; gridV(seq(-9,18,1)); gridH(seq(0,length(hm),1))
abovex("Heigths of a group of students - F/M")  #  F

Other examples:

Some population pyramids (a population pyramid illustrates the age and sex structure
of a country's population). 


       

CL=c("[0,5)","[5,10)","[10,15)","[15,20)","[20,25)", "[25,30)","[30,35)",
"[35,40)","[40,45)","[45,50)","[50,55)","[55,60)","[60,65)","[65,70)",
"[70,75)","[75,80)","[80,85)","[85,90)","[90,95)","[95,100)")
#
# The population pyramid of Western Europe in 2016:
m=c(26,26,27,28,29,31,32,30,31,37,39,35,31,27,22,18,12,6,2,0)/10
f=c(25,25,26,26,28,31,32,30,32,37,39,36,32,29,24,23,17,12,6,1)/10
BF=4.5; HF=4
pyramid(f,m, "pink","cyan",CL,"  ")
coldash="black"; GridV(c(-max(f),max(m))); gridV(seq(-3,3,1))
abovex("Western Europe 2016 - F/M population - %")
#
# The population pyramid of Western Europe in 1950:
m=c(41,37,41,37,37,35,23,33,36,36,30,23,21,17,14, 8,5,1)/10
f=c(39,36,39,36,37,41,27,40,41,39,36,31,27,22,18,11,7,2)/10
BF=4.5; HF=3.7
pyramid(f,m,"pink","cyan",CL,"  ")
coldash="black"; GridV(c(-max(f),max(m))); gridV(seq(-4,4,1))
abovex("Western Europe 1950 - F/M population - %")
#
# The population pyramid of Bahrain in 2016:
m=c(39,38,32,36,58,85,86,64,58,44,32,24,10,6,3,2,1)/10
f=c(38,35,32,29,30,37,40,33,29,25,20,14,7,4,3,3,1)/10
BF=4.5; HF=3.5
pyramid(f,m,"pink","cyan",CL,"  ")
coldash="black"; GridV(c(-max(f),max(m))); gridV(seq(-4,9,1))
abovex("Bahrain 2016 - F/M population - %")
#
# If I do not want the numbers under the histograms, I put "0" at the end:
pyramid(f,m,"pink","cyan",CL1,"0")
abovex("Bahrain 2016 - F/M population")

The number of people who move into Bahrain (to which they are not native) in order to
settle there as permanent residents (immigrants) will prevail over the number of people
who leave the country to settle permanently in another country (emigrants).
For other population pyramids of the world from 1950 to 2100 see:
https://www.populationpyramid.net


If the classes are of different sizes you can use pyramid2:
pyramid2(d1,d2, EI, co1,co2, CL,S)
EI is the collection of the extremes of the intervals.

  

                 

The male and female population of Italy in 1901 classified in the intervals:
[0,5), [5,10), [10,20), [20,35), [35,50), [50,60), [60,70), [70,100):

M = c(2099647, 1813237, 3485962, 3097280, 2659075, 1428007,  979685, 575429)
F = c(2016864, 1751544, 3483370, 3243667, 2739288, 1473875, 1015420, 583682)
EI = c(0,5,10,20,35,50,60,70,100)
#
M1=M/(sum(M)+sum(F))*100; F1=F/(sum(M)+sum(F))*100
BF=5;HF=3.5
pyramid2(F1,M1, EI, "pink","cyan",c(0,5,10,20,35,50,60,70,100),"")
coldash="black"; gridV(seq(-1.2,1.2,0.1))
abovex("Italy 1901 - F/M population - %")
# 1.294 is the unitary % frequence: the males with age in "one year" between 0 and 5
# are  1.294%  of the population
#
pyramid2(F,M, EI, "pink","cyan",c(0,5,10,20,35,50,60,70,100),"")
coldash="black"; gridV(seq(-3e5,4e5,1e5))
abovex("Italy 1901 - F/M population ")
#
pyramid2(F,M, EI, "pink","cyan",c(0,5,10,20,35,50,60,70,100),"")
coldash="black"; gridV(seq(-3e5,4e5,1e5))
abovex("Italy 1901 - F/M population ")
#
pyramid2(F,M, EI, "pink","cyan",c(0,5,10,20,35,50,60,70,100),"0")
abovex("Italy 1901 - F/M population")

We can study the male+female population with histoclass:

freq = M+F; histoclas(EI, freq)
              
morestat()
              

Obviously I can construct double histograms of data classified in non-numerical classes:

# Students enrolled in school or university by level of education (INF: age < 6 years, PRIM:
# age 6-10, SEC/UN: age > 10), and gender - Values in thousands

    

BF=4; HF=2.5
sf=c(499, 2099, 469+60); sm=c(491, 2344, 743+167)
CL = c("INF","PRIM","SEC/UN")
pyramid(sf,sm,"pink","cyan",CL," ")
coldash="brown"; gridV(seq(-2000,2000,500))
abovex("Students/1000 - Italy - 1951 - F/M")
#
sf=c(829, 1428, 2249+755); sm=c(768, 1345, 2129+947)
CL = c("INF","PRIM","SEC/UN")
pyramid(sf,sm,"pink","cyan",CL," ")
coldash="brown"; gridV(seq(-3000,3000,500))
abovex("Students/1000 - Italy - 2001 - F/M")