3

Wanting to show the distribution of participants in a survey by level, I came upon the recently-released pyramid package and tried it. As the font on the x-axis is too large and there seem to be no other formatting choices to fix it, I realized I don't know how to add "other options" as permitted by the ... in the pyramid call.

install.packages("pyramid")
library(pyramid)
level.pyr <- data.frame(left = c(1, 4, 6, 4, 41, 17),
                        right = c(1, 4, 6, 4, 41, 17),
                        level = c("Mgr", "Sr. Mgr.", "Dir.", "Sr. Dir.", "VP", "SVP+"))
pyramid(level.pyr, Laxis = seq(2,50,6), Cstep = 1, Cgap = .5, Llab = "", Rlab = "", Clab = "Title", GL = T, Lcol = "deepskyblue", Rcol = "deepskyblue", Ldens = -1, main = "Distribution of Participants in Survey")

Agreed, the plot below looks odd because the left and the right sides are the same, not male and female. But my question remains as to how to invoke the options and do something like "Laxis.size = 2" of "Raxis.font = "bold".

enter image description here

Alternatives to this new package for creating pyramid plots include plotrix, grid, and base R, as demonstrated here: population pyramid density plot in r By the way, if there were a ggplot method, I would welcome trying it.

Community
  • 1
  • 1
lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • The documentation of that package could be improved. Looking at the function's source code we can see that the ellipses arguments are passed to `plot`. So, you can use the same arguments that `plot` accepts. – Roland Dec 24 '14 at 20:39
  • When I run you code, I am able to display the graph without the lower axis labels cut off by adjusting the size of my plot window in RStudio. I'm not sure exactly which option(s) you would pass to `plot` via `...` to do this programmatically, but I'm sure it can be done. – nrussell Dec 24 '14 at 20:43

1 Answers1

3

Contrary to Roland's and now nrussell's guesses (without apparently looking at the code) expressed in comments, the dots arguments will not be passed to pyramid's axis plotting routine, despite this being a base graphics function. The arguments are not even passed to an axis call, although that would have seemed reasonable. The x-axis tick labels are constructed with a call to text(). You could hack the text calls to accept a named argument of your choosing and it would be passed via the dots mechanism. You seem open to other options and I would recommend using plotrix::pyramid.plot since Jim Lemon does a better job of documenting his routines and it's more likely they will be using standard R plotting conventions:

library(plotrix)
pyramid.plot(lx,rx,labels=NA,top.labels=c("Male","Age","Female"),
  main="",laxlab=NULL,raxlab=NULL,unit="%",lxcol,rxcol,gap=1,space=0.2,
  ppmar=c(4,2,4,2),labelcex=1,add=FALSE,xlim,show.values=FALSE,ndig=1,
  do.first=NULL)


with( level.pyr, pyramid.plot(lx=left, rx=right, labels=level, 
        gap =5, top.labels=c("", "Title", ""), labelcex=0.6))

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 4
    Thank you, BondedDust. Lesson one: look at the function's code first to see what the optional arguments are even passed to! Lesson two: between two seemingly equal choices of capabilities, stick with the better documented package. Lesson three: R seems almost always to have multiple solutions! Lesson four: SO always delivers more than expected. – lawyeR Dec 24 '14 at 21:44
  • 1
    Bravo! Your capacity for learning is excellent. I predict a rapid increase in your R capabilities. – IRTFM Dec 25 '14 at 00:14