r - Put two values on top of each bar in a histogram -
i have next data,
sampleid pos dep pvalues sample_1 849 62 0.02755358 sample_1 859 63 0.07406833 sample_1 864 63 0.00351564 sample_1 883 60 0.02780868 sample_1 893 58 0.00451450 sample_1 895 58 0.03600795 sample_2 54 66 0.11864407 sample_2 55 67 0.01515152 sample_2 71 91 0.02712367 sample_2 78 97 0.00077325
i have generated histogram of p-values frequency values on top of each bar. below, code
pval_at_site <- read.table("samples.pval") s <- hist(pval_at_site$pvalues, xlab="pval",cex=0.8) text(s$mids,s$counts,s$count,srt=90,pos = 3,offset=1,cex=0.6)
now, is, along p-value frequency, add together number of samples on top of each bar.
for example, if have, say, 1000 datapoints in first interval, , these values come 20 unique samples want plot "1000,20" on top of first bar.
please allow me know how should go this. hope have made myself clear.
thanks.
you can compute number of unique values, , generate text labels outside hist()
computations. there more efficient ways split-apply-combine operation (look dplyr
, data.table
), code below implements minimal changes:
data= "sampleid pos dep pvalues sample_1 849 62 0.02755358 sample_1 859 63 0.07406833 sample_1 864 63 0.00351564 sample_1 883 60 0.02780868 sample_1 893 58 0.00451450 sample_1 895 58 0.03600795 sample_2 54 66 0.11864407 sample_2 55 67 0.01515152 sample_2 71 91 0.02712367 sample_2 78 97 0.00077325" pval_at_site <- read.table(text=data, header=true) s <- hist(pval_at_site$pvalues, xlab="pval",cex=0.8) # vector of each bin bins <- cut(pval_at_site$pvalues, breaks=s$breaks) # sum of unique values bin value based on hist() output count.samples <- tapply(pval_at_site$sampleid, bins, function(x) length(unique(x))) count.samples[is.na(count.samples)] <- 0 ## remove nas empty bins # generate text labels combining both values tags <- paste(s$count, count.samples, sep=" - ") text(s$mids,s$counts,tags,srt=90,pos = 3,offset=1,cex=0.6)
r histogram plotrix
No comments:
Post a Comment