[R] trouble with histograms
Peter Ehlers
ehlers at ucalgary.ca
Wed Oct 27 11:57:55 CEST 2010
On 2010-10-26 22:00, Marcel Curlin wrote:
>
> Hi,
> I have tab-delimited data with an unequal number of entries per column, of
> the sort:
>
> A B C
> 1 2 2
> 3 4 1
> 5 2 2
> 6 2
> 5 2
> 3
> 6
> 2
>
> I would like to make a histogram of the frequencies of each represented
> number in a "stacked" histogram, where you can see the contribution of each
> group (A, B or C) to the total height of the bar, and each bar labeled with
> the represented number. So, there would be a bar labeled "1" of height 2,
> half one color for group A, and half another color for group B.
>
> So far,
> I can get my data into a dataframe
>> data<- read.table("myfile")
>
> I think I first have to use "hist" to get the frequencies of each, and I
> have figured out how to use breaks to make bins;
>> bins=seq(0.5,6.5,by=1)
>> hist(data$A, header=T, sep="\t", breaks=bins)
>
> Lots of trouble from then on, though, and I just can't get this into a
> usable plot. Any help appreciated.
>
> Marcel
Maybe I'm misunderstanding, but I think that you want
a barplot rather than a histogram. Try this:
DF <- read.table(textConnection(
"A B C
1 2 2
3 4 1
5 2 2
6 2
5 2
3
6
2"), header=TRUE, fill=TRUE)
closeAllConnections()
DF <- stack(DF)
DF
## use xtabs() to tabulate the values
xtDF <- xtabs( ~ ., DF)
## transpose so that columns are the heights to
## feed to barplot()
heights <- t(xtDF)
barplot(heights, col=2:4)
-Peter Ehlers
More information about the R-help
mailing list