Reputation: 64004
I have a data that looks like this
ensg mirna_hgc time value perc id
ENSG00000211521 MIR665 x 89 2.07612456747405 1
ENSG00000207787 MIR98 x 73 1.73010380622837 2
...
ENSG00000207827 MIR30A y 99 21.4532871972318 288
ENSG00000207757 MIR93 y 94 1.73010380622837 289
What I'm trying to do is to create a facet plot with label on top of it.
The label can be easily called from the perc
column.
Using this code:
dat.m <- read.delim("http://dpaste.com/1271039/plain/",header=TRUE,sep=" ")
qplot(value, data=dat.m,facets=time~.,binwidth=1,main="")+
xlab("Value")+ ylab("Count")+
theme(legend.position="none")+
stat_bin(aes(value,label=sprintf("%.01f",perc)),geom="text")
But it gave me this error:
Error: geom_text requires the following missing aesthetics: label
What I'm trying to do is to generate this plot:
Upvotes: 2
Views: 2256
Reputation: 115392
Your issue arises in part
because you are using qplot
(which makes things more confusing than you need).
You have set binwidth = 2
which means each histogram bar is based (possibly) on 2 values of value. Your 1-to-1 mapping of perc to value is now 2 -to -1 mapping of perc to the binned values
Using plyr
to continue the hadleyesque implementation we can aggregated before we plot
library(plyr)
agg.data <- ddply(dat.m, .(value,time), summarize, p = unique(perc), n = length(perc))
ggplot(agg.data, aes(x= value)) +
geom_bar(aes(y = n),stat='identity') +
facet_grid(time~.) +
geom_text(aes(y=n, label = sprintf('%.01f',p)),vjust=-1,size=3)
If you want to use geom_histogram
and display the proportions associated with each bar, this can be done quite easily, but you will need to use the values created by stat_bin
(namely ..density..
, which is proportion allocated to each bar)
ggplot(dat.m, aes(x= value)) +
geom_histogram(binwidth=2) +
facet_grid(time~.) +
stat_bin(aes(y = ..count.., label = sprintf('%.01f', 100 * ..density..)),
binwidth=2,geom='text',vjust = -1)
and to replace the 0.0
with no labels use ifelse
(with some definition trickery to avoid replicating the test.
ggplot(dat.m, aes(x= value)) +
geom_histogram(binwidth=2) + facet_grid(time~.) +
stat_bin(aes(y = ..count..,
label = ifelse(test = (xxx <- sprintf('%.01f', 100 * ..density..))=='0.0','',xxx)),
binwidth=2,geom='text',vjust = -0.7)
And note that you could get the same thing using qplot
using
qplot(value, data=dat.m,facets=time~.,binwidth=2,main="")+
xlab("Value")+ ylab("Count")+
theme(legend.position="none")+
stat_bin(aes(value,label=ifelse(test = (xxx <-
sprintf("%.01f",..density..*100))=='0.0','',xxx)),geom="text",binwidth=2,vjust = -0.7)
Upvotes: 4