1

I am trying to get geom_bar to divide counts by a normalizing factor rather than dividing by sum(..counts..) e.g.

n=200
df = data.frame(let = letters[sample(1:26,n,replace=TRUE)],
    cat=letters[sample(1:2,n,replace=TRUE)],
    norm = as.integer(1+round(runif(n)*10))

d <- ggplot(df, aes(let,fill=cat)) +
    geom_bar(aes(y = ((..count..)/sum(..count..))),position='dodge')

enter image description here

Instead I would like to divide by a normalizing factor:

d <- ggplot(df, aes(let,fill=cat)) +
    geom_bar(aes(y = ((..count..)/norm)),position='dodge')

But this produces an error:

> d
Error in (count)/norm : non-numeric argument to binary operator

This is just a toy example. My actual code has a different error which I have yet failed to replicate:

> ggplot(droplevels(dfR[keep,]), aes(x=loc_breakBinned,fill=amalgamated_group) ) + 
     geom_bar(aes(y = ((..count..)/subject_count_ASDvTD)),position='dodge')
Error in eval(expr, envir, enclos) : 
  object 'subject_count_ASDvTD' not found

ggplot2 insists that subject_count_ASDvTD is not part of dfR but a quick looks shows that it clearly is:

> str(dfR[keep,c('amalgamated_group','loc_breakBinned','subject_count_ASDvTD')])
'data.frame':   3694 obs. of  3 variables:
 $ amalgamated_group   : Factor w/ 6 levels "ASD","CONTRAST",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ loc_breakBinned     : Factor w/ 18 levels "pos:(10.7,12.8]_totExonD:(-0.00822,1.64]",..: 14 1 8 8 14 4 8 13 8 14 ...
 $ subject_count_ASDvTD: int  213 213 213 213 213 213 213 213 213 213 ...

What is going on here? Why cant ggplot see subject_count_ASDvTD?

Note: the same error arrises from

ggplot(droplevels(dfR[keep,]), aes(x=loc_breakBinned,fill=amalgamated_group,y = ((..count..)/subject_count_ASDvTD) ) ) + 
    geom_bar(position='dodge')
aosmith
  • 34,856
  • 9
  • 84
  • 118
user3030872
  • 367
  • 1
  • 6
  • 14
  • Is it possible that you just want to weight, using `weight = 1/norm` in `aes`? I don't see how you could divide the counts (which is aggregated for each group) by something that hasn't been aggregated (like `norm`). [This question](http://stackoverflow.com/questions/17832608/how-to-use-earlier-declared-variables-within-aes-in-ggplot-with-special-operator) could be related, although that's dividing by a constant. Another alternative is to just calculate the value you want to graph outside of ggplot2 - that would be straightforward. – aosmith Mar 31 '16 at 17:48
  • I don't think weight is what I'm looking for. I'm trying to get: number of loci observed / subject within group_i of amalgamated groups – user3030872 Mar 31 '16 at 17:58
  • I'm not sure weight is what I'm looking for. I'm trying to get: number of loci observed / subject within group_i of amalgamated_groups. Percent gives me: number of loci / number of loci within group_i of amalgamated_groups. Would weight=subject_count would give me: number subjects within group_i of amalgamated_groups / number of loci within group_i of amalgamated_groups ? That is the inverse of what I'm looking for. Then would this work? `ggplot(droplevels(dfR[keep,]), aes(x=subject_count_ASDvTD,fill=amalgamated_group,weight=loc_breakBinned ) + geom_bar(position='dodge')` – user3030872 Mar 31 '16 at 18:04
  • Nevermind that other answer worked. Thanks @aosmith – user3030872 Mar 31 '16 at 18:06

1 Answers1

0

The solution is the define the variable within aes. Thanks to @aosmith for helping me sort that out. Corrected versions of the above code can be found below:

d <- ggplot(df, aes(let,fill=cat,norm=norm)) +
    geom_bar(aes(y = ((..count..)/norm)),position='dodge')

enter image description here

And the more complex actual code:

ggplot(droplevels(dfR[keep,]), aes(subject_count=subject_count_ASDvTD,x=loc_breakBinned,fill=amalgamated_group,y = ((..count..)/subject_count) ) ) + 
    geom_bar(position='dodge')
user3030872
  • 367
  • 1
  • 6
  • 14