Histograms in R

classic Classic list List threaded Threaded
3 messages Options
lt2
Reply | Threaded
Open this post in threaded view
|

Histograms in R

lt2
HI everyone,
I'm plotting a histogram in R and within that histogram i need to demonstrate the percentage of another variable (Percentage of MutStatus) within the bins plotted inthe histogram....I don't know how to do that!

Data:Validation_Status Mutation_Status TvarRatio
Wildtype None 0.08
Wildtype None 0.08
Wildtype None 0.08
Wildtype None 0.08
Wildtype None 0.080139373
Wildtype None 0.080152672
Wildtype None 0.080213904
Wildtype None 0.080357143
Wildtype None 0.080357143
Wildtype None 0.080357143
Wildtype None 0.08045977
Wildtype None 0.1
Wildtype LOH 0.1
Wildtype None 0.1
Wildtype None 0.1
Wildtype None 0.1
Wildtype None 0.1
Wildtype None 0.1
Wildtype Somtatic 0.1
Wildtype None 0.100558659
Wildtype None 0.100591716
Wildtype None 0.101010101
Wildtype None 0.101123596
Wildtype Gline 0.101333333
Wildtype None 0.101369863
Wildtype None 0.101449275
Wildtype None 0.101522843
Wildtype None 0.101604278
Wildtype None 0.102040816
Wildtype Gline 0.102040816
Wildtype None 0.102362205
Wildtype None 0.102459016
Wildtype None 0.102564103
Wildtype None 0.102702703
Wildtype None 0.102739726
Wildtype None 0.102803738
Valid Somatic 0.102941176
Wildtype None 0.102941176
Reply | Threaded
Open this post in threaded view
|

Re: Histograms in R

Michael Weylandt
Perhaps you could shade the bars as appropriate?

I'm not going to use your data because it's not an easily paste-able but how
about this:

x = rnorm(100)
y = sample(c("A","B"),100,replace=T,prob=c(0.7,0.3))

d = data.frame(level = x, status = y)
n = 10 # Number of bins
breaks = quantile(d$level, (0:n)/n)
#breaks = with(d,hist(level,breaks=n,plot=F)$breaks)
breaksAssign = findInterval(d$level,breaks)

percentB = unique(ave(d$status=="B",breaksAssign)[order(breaksAssign)])
percentB = gray(percentB/max(percentB)) # Many other color functions
available as well.

with(d, hist(level, breaks = breaks, col = percentB))

If you want to use hist()'s smart choice of bins (which I'd recommend), you
can call this hist command once with plot=F and get the breaks from there.
I.e., breaks = with(d,hist(level,breaks=n,plot=F)$breaks)

There's probably a smarter way to do all this, but this does seem to work...

Hope this helps,

Michael Weylandt

On Wed, Aug 10, 2011 at 1:16 PM, lt2 <[hidden email]> wrote:

> HI everyone,
> I'm plotting a histogram in R and within that histogram i need to
> demonstrate the percentage of another variable (Percentage of MutStatus)
> within the bins plotted inthe histogram....I don't know how to do that!
>
> Data:Validation_Status  Mutation_Status TvarRatio
> Wildtype        None    0.08
> Wildtype        None    0.08
> Wildtype        None    0.08
> Wildtype        None    0.08
> Wildtype        None    0.080139373
> Wildtype        None    0.080152672
> Wildtype        None    0.080213904
> Wildtype        None    0.080357143
> Wildtype        None    0.080357143
> Wildtype        None    0.080357143
> Wildtype        None    0.08045977
> Wildtype        None    0.1
> Wildtype        LOH     0.1
> Wildtype        None    0.1
> Wildtype        None    0.1
> Wildtype        None    0.1
> Wildtype        None    0.1
> Wildtype        None    0.1
> Wildtype        Somtatic        0.1
> Wildtype        None    0.100558659
> Wildtype        None    0.100591716
> Wildtype        None    0.101010101
> Wildtype        None    0.101123596
> Wildtype        Gline   0.101333333
> Wildtype        None    0.101369863
> Wildtype        None    0.101449275
> Wildtype        None    0.101522843
> Wildtype        None    0.101604278
> Wildtype        None    0.102040816
> Wildtype        Gline   0.102040816
> Wildtype        None    0.102362205
> Wildtype        None    0.102459016
> Wildtype        None    0.102564103
> Wildtype        None    0.102702703
> Wildtype        None    0.102739726
> Wildtype        None    0.102803738
> Valid   Somatic 0.102941176
> Wildtype        None    0.102941176
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Histograms-in-R-tp3733644p3733644.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Histograms in R

Adams, Jean
In reply to this post by lt2
Assuming your data is in a data.frame called df, try this:

attach(df)
TR.groups <- cut(TvarRatio, seq(0.07, 0.11, 0.01))
m <- table(Mutation_Status, TR.groups)
mut.no <- dim(m)[1]
barplot(m, col=seq(mut.no), xlab="TvarRatio", ylab="Frequency")
legend("topleft", dimnames(m)[[1]], fill=seq(mut.no),
title="Mutation_Status")

Jean

`·.,,  ><(((º>   `·.,,  ><(((º>   `·.,,  ><(((º>

Jean V. Adams
Statistician
U.S. Geological Survey
Great Lakes Science Center
223 East Steinfest Road
Antigo, WI 54409  USA

[hidden email] wrote on 08/10/2011 12:16:51 PM:

> [image removed]
>
> [R] Histograms in R
>
> lt2
>
> to:
>
> r-help
>
> 08/10/2011 12:18 PM
>
> Sent by:
>
> [hidden email]
>
> HI everyone,
> I'm plotting a histogram in R and within that histogram i need to
> demonstrate the percentage of another variable (Percentage of MutStatus)
> within the bins plotted inthe histogram....I don't know how to do that!
>
> Data:Validation_Status   Mutation_Status   TvarRatio
> Wildtype   None   0.08
> Wildtype   None   0.08
> Wildtype   None   0.08
> Wildtype   None   0.08
> Wildtype   None   0.080139373
> Wildtype   None   0.080152672
> Wildtype   None   0.080213904
> Wildtype   None   0.080357143
> Wildtype   None   0.080357143
> Wildtype   None   0.080357143
> Wildtype   None   0.08045977
> Wildtype   None   0.1
> Wildtype   LOH   0.1
> Wildtype   None   0.1
> Wildtype   None   0.1
> Wildtype   None   0.1
> Wildtype   None   0.1
> Wildtype   None   0.1
> Wildtype   Somtatic   0.1
> Wildtype   None   0.100558659
> Wildtype   None   0.100591716
> Wildtype   None   0.101010101
> Wildtype   None   0.101123596
> Wildtype   Gline   0.101333333
> Wildtype   None   0.101369863
> Wildtype   None   0.101449275
> Wildtype   None   0.101522843
> Wildtype   None   0.101604278
> Wildtype   None   0.102040816
> Wildtype   Gline   0.102040816
> Wildtype   None   0.102362205
> Wildtype   None   0.102459016
> Wildtype   None   0.102564103
> Wildtype   None   0.102702703
> Wildtype   None   0.102739726
> Wildtype   None   0.102803738
> Valid   Somatic   0.102941176
> Wildtype   None   0.102941176
>
> --
> View this message in context: http://r.789695.n4.nabble.com/
> Histograms-in-R-tp3733644p3733644.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.