---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
 Histograms 3D and mosaic graphs.

# Employed persons in Italy at 2012:
#            males  females
# primary     603     246
# secondary  5051    1311
# tertiary   7787    7901
da = c(603,5051,7787, 246,1311,7901)
T = array(da, dim=c(3,2)); T
     [,1] [,2]
[1,]  603  246
[2,] 5051 1311
[3,] 7787 7901
# If I want the percentage distribution (and the names)
name = list(c("primary","secondary","tertiary"), c("males","females"))  # 3×2
T1 = array(round(da/sum(da)*100,2), dim=c(3,2) , dimnames=name); T1
          males females
primary    2.63    1.07
secondary 22.06    5.73
tertiary  34.01   34.50
# I add the names to T:
T2 <- array(da, dim=c(3,2),dimnames=name); T2
          males females
primary     603     246
secondary  5051    1311
tertiary   7787    7901
# The marginal distribution of the sex
colSums(T2)/sum(T2)*100
   males  females 
58.69689 41.30311 
round(colSums(T2)/sum(T2)*100,3)
  males females 
 58.697  41.303

# barplot:
bar(T[,1]); bar(T[,2])
# %  4.486273 37.57905 57.93468 
# %  2.600973 13.86128 83.53775
# on the same reference system (see):
S = c(4.486273, 37.57905, 57.93468, 0, 2.600973, 13.86128, 83.53775)
co = c("green","cyan","grey"); col=(c(co,0,co))
BARM(S,col,85,(1:8)*10)
underX(c("%","prim","seco","tert","prim","seco","tert"),c(-0.5,0.5,1.5,2.5,4.5,5.5,6.5))
aboveX(c("M","F"),c(1.5,5.5))
  

# The histogram:
histo3D(T,-50,30,1)              # the graph on the left:



# -50, 30 (or other values) indicate the position of the eye: the direction (teta) of
# the look projected on the horizontal plane and its inclination (fi) relative to that
# plane.  1 indicate to trace the box.
histo3D(T,-50,30,0)
# With 0 I do not trace the box (graph in the center).
# The strings "I II III" and "F M" are written in the point xP,yP where I click
# after I typed PPP()
# 
PPP()
text(xP,yP,"I II III")
PPP()
text(xP,yP,"F M")
#
# The graph on the right is called "mosaic plot".
# It is a convenient alternative to the 3D histogram.
da = c(603,5051,7787, 246,1311,7901)
name <- list(c("I","II","III"),c("M","F"))
dat = array(da,dim=c(3,2), dimnames=name)
mosaicplot(dat)
#
# Some variants.
 
# If instead of 0 (or 1) at the end I use 0.n (or 1.n) I reduce the width of the
# columns (a little if n is 1, 2, … a lot if n is 9, 8, …)
# If instead of teta (50 in this case) I use teta.n I reduce the height of the columns
isto3D(T,-50.4,30,0.3)              # the first graph
isto3D(T,-50.7,30,0)                # the second graph
# If instead of fi (30 in this case) I use fi.1 I draw only the roofs of the columns
isto3D(T,-50.4,30.1,0)
# If instead of 0 (or 1) at the end I use 0.01 (or 1.01) I obtain a polygon mesh
# (the vertices are the centers of the rectangles)
isto3D(T,-50.4,30,1.01)
# If I put DIST=1 the distance of the eye is great
DIST=1; isto3D(T,200.4,10,1.01)
          

# Other commands for the tables
 
prop.table(T)
           [,1]       [,2]
[1,] 0.02633303 0.01074283
[2,] 0.22057732 0.05725141
[3,] 0.34005852 0.34503690
 
prop.table(T1)
           males females
primary   0.0263  0.0107
secondary 0.2206  0.0573
tertiary  0.3401  0.3450
 
round(prop.table(T1)*100,1)
          males females
primary     2.6     1.1
secondary  22.1     5.7
tertiary   34.0    34.5
 
T1[1,]
  males females 
   2.63    1.07
 
T1[,2]
  primary secondary  tertiary 
     1.07      5.73     34.50
 
# The distribution of 1st and 2nd col
 
round(prop.table(T1[,1])*100,1)
  primary secondary  tertiary 
      4.5      37.6      57.9
 
round(prop.table(T1[,2])*100,1)
  primary secondary  tertiary 
      2.6      13.9      83.5 

Other examples of use