Fred J.
Fred J.

Reputation: 21

Histogram of binned data frame in R

My (huge) dataframe coming from a python code is composed of counts in different size classes for each sample as in :

dummy <- as.data.frame(matrix(nrow = 10, ncol = 12))
colnames(dummy) <- c("ID", paste("cl", c(1:11), sep = "."))
dummy$ID <- c(letters[1:10])
dummy[, -1] <- rep(round(abs(rnorm(11))*1000,0), 10)

I try to create histograms of the counts for each sample (ID) having size classes on X axis and counts (frequencies) on Y axis. No success with hist(), combining as.numeric() and t() and as.table() ...

I don't succeed in telling R that this data frame is (at least partly) a table with counts already distributed in bins that are the colnames. I'm sure I'm not the first guy looking for that but can't find the answer since two days, maybe because I don't get the right keywords (?).

Can somebody help?

Upvotes: 2

Views: 3975

Answers (2)

MurrayStokely
MurrayStokely

Reputation: 345

ggplot2 is nice for questions like this, but you can do something with base R graphics as well. It's not super pretty, but for example I would do :

par(mfrow=c(5,2))
par(mar=c(2,2,2,1))
for (i in 1:nrow(dummy)) {
  barplot(as.numeric(dummy[i,-1]), names.arg=colnames(dummy[1,-1], main=dummy[i,1])
}

enter image description here

Upvotes: 2

Roland
Roland

Reputation: 132999

A histogram is basically a special kind of barplot. So you could just use function barplot.

I prefer package ggplot2 for this:

#reshape to long format
library(reshape2)    
dummy <- melt(dummy, id.var="ID")

library(ggplot2)    
p <- ggplot(dummy, aes(x=variable, y=value)) + 
  geom_histogram(stat="identity") + 
   #specifying stat_identity tells ggplot2 that the data is already binned
  facet_wrap(~ID, ncol=2)

print(p)

enter image description here

Upvotes: 3

Related Questions