我想根据位置绘制长度的直方图。我试图覆盖直方图,其中一个位置的数据是一种颜色而另一个位置是不同的颜色。
这是我到目前为止只用直方图绘制的R代码:
fasta<-read.csv('fastadata.csv',header = T)
norton<-fasta[fasta$SampleID == ">P.SC1Norton-28F",]
cod<-fasta[fasta$SampleID == ">P.SC4CapeCod-28F ",]
bins <- seq(200, 700, by=25)
hist(fasta[,3], breaks=bins, main="Histogram of ReadLengths of a set bin size for Cape Cod and Norton", xlab="ReadLengths")
我一直看到使用ggplot,但我不确定如何在一个表中使用此函数并使用我使用的binning。
dput(head(fasta))
的输出:
structure(list(SampleID = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c(">P.SC1Norton-28F",">P.SC4CapeCod-28F"), class = "factor"), SeqName = structure(c(5674L, 5895L, 5731L, 5510L, 4461L, 5648L), .Label = c("IJO4WN203F00DQ", "IKTXKCP03HKQ5E"), class = "factor"), ReadLength = c(394L, 429L, 437L, 438L, 459L, 413L)), .Names = c("SampleID", "SeqName", "ReadLength"), row.names = c(NA, 6L), class = "data.frame")
答案 0 :(得分:5)
由于您提到了ggplot
,因此您有多种选择。
# make up some data
set.seed(1)
sampleID <- c(">P.SC1Norton-28F",">P.SC4CapeCod-28F")
df <- data.frame(SampleID=rep(sampleID,each=500),
ReadLength=round(c(rnorm(500,350,100),rnorm(500,450,100))))
library(ggplot2)
ggplot(df) +
geom_histogram(aes(x=ReadLength, fill=SampleID),
colour="grey50", alpha=0.5, position="identity")
ggplot(df) +
geom_histogram(aes(x=ReadLength, fill=SampleID), position="dodge")
ggplot(df) +
geom_histogram(aes(x=ReadLength, fill=SampleID))+
facet_wrap(~SampleID,nrow=2)
答案 1 :(得分:4)
在add=TRUE
的第二次通话中使用hist
参数。此外,使用alpha透明色可能会有所帮助。
hist(norton[,3], breaks=bins, main="Histogram of ReadLengths of a set bin size for Cape Cod and Norton",
xlab="ReadLengths", col=rgb(1,0,0,.5), border=NA)
hist(cod[,3], breaks=bins, col=rgb(0,0,1,.5), add=TRUE, border=NA)
这是使用@ jlhoward数据的更新。请注意,默认情况下,轴标签和标题会很杂乱:
layout(1:2)
hist(df$ReadLength[df$SampleID==levels(df$SampleID)[1]],
col=rgb(1,0,0,.5), border=NA)
hist(df$ReadLength[df$SampleID==levels(df$SampleID)[2]],
col=rgb(0,0,1,.5), border=NA, add=TRUE)
hist(df$ReadLength[df$SampleID==levels(df$SampleID)[1]],
col=rgb(1,0,0,.5), border=NA)
hist(df$ReadLength[df$SampleID==levels(df$SampleID)[2]],
col=rgb(0,0,1,.5), border=NA)