Histogram with varying-width bars -- R code

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Histogram with varying-width bars -- R code

H. Lawrence Hotchkiss
Getting a histogram (in this case, a bar chart) with varying-width bars is so
easy with R that it might be worth exporting your data to R and trying it. Here
is sample code using the data you posted:

 #        Age         Freq
 #        ---       ---------
 #        0-4           28
 #        5-9           46
 #      10-15           58
 #         16           20
 #         17           31
 #      18-19           64
 #      20-24          149
 #      25-59          316

  age <- c(28,46,58,20,31,64,149,316)
  names(age) <- c("0-4","5-9","10-15","16","17","18-19","20-24","25-59")
  width <- c(5,5,6,1,1,2,5,35)

 # Plain vanilla barplot
  barplot(age/width, width=width)
 # Embellished version with colors. The abline(0,0) function puts the
 # horizontal line on the x axis
  barplot(age/width, width=width, col="beige", border="brown")
  abline(0,0)

(The # are comments.)  Note that age is divided by the width of the intervals.

If your data are not already grouped (e.g., you have age in years rather than in
intervals of years), use the hist function with the probability=TRUE option,
like this --

   hist(age, probability=TRUE, breaks=c(...))

substituting the comma-separated boundaries you want for ... inside of c(...).

Larry Hotchkiss




Larry Hotchkiss
University of Delaware
IT User Services -- Smith Hall
Newark, DE 19716
302-831-1989  [hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD