[R] hist for two sets

Marc Schwartz MSchwartz at mn.rr.com
Fri Sep 15 01:55:34 CEST 2006


On Thu, 2006-09-14 at 19:37 -0400, Ethan Johnsons wrote:
> A quick question, please.
> 
> x = c(0.0001, 0.0059, 0.0855, 0.9082)
> y = c(0.54, 0.813, 0.379, 0.35)
> 
> where A = 1st set, B = 2nd set, C = 3rd set, D = 4th set respectivley.
> 
> How do you make hist plot side by side for x & y?
> 
> i.e. 0.0001 and then to the right 0.54, 0.0059 and then to the right 0.813,
> etc.
> 
> thx much

You don't want a histogram, but a barplot:

 x <- c(0.0001, 0.0059, 0.0855, 0.9082)
 y <- c(0.54, 0.813, 0.379, 0.35)

 # create a two row matrix with x and y
 height <- rbind(x, y)

 # Use height and set 'beside = TRUE' to get pairs
 # save the bar midpoints in 'mp'
 # Set the bar pair labels to A:D
 mp <- barplot(height, beside = TRUE, ylim = c(0, 1), 
               names.arg = LETTERS[1:4])

 # Draw the bar values above the bars
 text(mp, height, labels = format(height, 4), pos = 3, cex = .75)


See ?barplot, ?text and ?format (or ?formatC or ?sprintf).

HTH,

Marc Schwartz



More information about the R-help mailing list