R中的数据帧的直方图 [英] Histogram of binned data frame in R
问题描述
我的(巨大的)数据帧来自一个python代码,由不同大小类别的计数组成,如下所示:
dummy < - as.data.frame(matrix(nrow = 10,ncol = 12))
colnames(dummy)< - c(ID,paste(cl,c(1:11 )
dummy $ ID < - c(letters [1:10])
dummy [,-1]< - rep(round(abs(rnorm(11 ))* 1000,0),10)
我尝试为每个样本创建计数的直方图(ID)在X轴上具有大小等级,并且在Y轴上具有计数(频率)。没有成功, hist()
,将 as.numeric()
和 t()
和 as.table()
...
我没有成功告诉R这个数据框(至少部分地)是一个已经分配在$ code> colnames 的bin中的表的表。我相信我不是第一个寻找这个,但两天后找不到答案的人,也许是因为我没有得到正确的关键字(?)。
$ b $有人可以帮忙吗?
直方图基本上是一种特殊的barplot。所以你可以使用函数 barplot
。
我更喜欢这个包ggplot2:
#reshape to long format
/ pre>
library(reshape2)
dummy< - melt(dummy,id.var =ID)
库(ggplot2)
p< - ggplot(dummy,aes(x = variable,y = value))+
geom_histogram(stat =identity)+
#specifying stat_identity告诉ggplot2数据已经被binned
facet_wrap(〜ID,ncol = 2)
print(p)
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()
, combiningas.numeric()
andt()
andas.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?
解决方案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)
这篇关于R中的数据帧的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!