[R] Christmas tree graphs

Mark Myatt mark at myatt.demon.co.uk
Mon Sep 24 16:43:50 CEST 2001


Andrew C. Ward wrote:

>> Hello R users,
>> 
>> I wish to draw a type of horizontal bar chart for two groups (males and 
>> females). The values for one group will start at the y-axis and point to the 
>> left, and the values for the other group will start at the y-axis and point 
>to 
>> the right. If you're (un)lucky, the resulting graph can assume the shape of a 
>> Christmas tree.
>> 
>> The data are
>>      left.side <- c(107092, 113053, 121163, 112209, 106543, 72895, 46920, 
>> 32606, 11106, 1134, 60, 5247)
>>      right.side <- c(102129, 109261, 123291, 117322, 110397, 71027, 50319, 
>> 43805, 21074, 3621, 204, 6608)
>> 
>> The R function barchart allows me to stack bars and to juxtapose them, but I 
>> can't see how to draw the bars outwards from a centreline. If anyone had some 
>> hints on how to do this, I would be very grateful. Thank you!

I have:

pyramid.plot <- function(x,

                         g,

                         main = paste("Pyramid plot of",  
                                      deparse(substitute(x)), "by",
                                      deparse(substitute(g))),

                         xlab = paste(deparse(substitute(g)), "(", 
                                      levels(g)[1], "/", 
                                      levels(g)[2],")"),

                         ylab = deparse(substitute(x))

                         )
  {
  tab <- table(x, g)
  tab[ ,1] <- -tab[ ,1]
  barplot(tab,
          horiz = TRUE,
          beside = TRUE,
          space = c(0, -nrow(tab)),
          names.arg = c(dimnames(tab)$x, dimnames(tab)$x),
          xlim = c(min(tab) * 1.1, max(tab) * 1.1),
          col = "white",
          main = main,
          xlab = xlab,
          ylab = ylab
          )
  }

Which does the trick for me for raw data rather than summary data. It
should be easy enough to adapt as the barplot()n function takes summary
data ... you need to construct a matrix from your summary data and give
it some dimnames for the agegroup variables.

Mark

--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list