Histogram of binned data frame in R

£可爱£侵袭症+ 提交于 2019-12-24 04:41:09

问题


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?


回答1:


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)




回答2:


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])
}



来源:https://stackoverflow.com/questions/18219704/histogram-of-binned-data-frame-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!