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')
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')