0

I have this data file:

A
    169
    164
    164
    102
    150
    100
    145
B
    145
    107
    115
    120
    140

I want to create a stacked histogram out of it. I'd like to set a range (let's say 100 to 170), set bin width (let's say 10) for X axis and that it will count how much it has in each bin (for example 140 to 149 would have 1 in A and 2 in B) and plot the count in Y axis using bars. The coloring (or hatching or whatever) would be different for the A count and B count. I tried looking at the histogram demos of gnuplot and it does seem similar in some cases, but I couldn't figure out how to make it work. Also no luck with python+matplotlib or R. Reordering the data to something like

 A B
 169 145
 164 107
 164 115
 102 120
 150 140
 100
 145

Is not a problem if needed.

Gimelist
  • 791
  • 1
  • 10
  • 25
  • 2
    What have you tried? Can you make a reproducible example? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Feb 24 '13 at 11:17
  • Well the closest I got to making something similar was playing with the gnuplot examples of the histograms. In there it seems that the data format is takes is already counted, that is I need to give it something like this: 100-109 110-119 etc.. 4 2 etc... But in that case I can do the counting myself in python (for example). I'm sort of looking for a simple solution. If you're familiar with GMT, similar to the pshistogram command, only with support for 2 or more data sets. – Gimelist Feb 24 '13 at 11:31

1 Answers1

7

It is quite easy to make a stacked histogram using Hadley Wickham's ggplot2 package in R:

library(ggplot2)
h <- data.frame(var=sample(letters[1:2], 100, TRUE), 
                value=rnorm(100, 0, 10))

ggplot(aes(x=value, color=var), data=h) + stat_bin(binwidth=5)

Removing the color= bit would produce a simple histogram.

driu
  • 108
  • 5