Show frequencies along with barplot in ggplot2
geom_text
is tha analog of text
from base graphics:
p + geom_bar() + stat_bin(aes(label=..count..), vjust=0,
geom="text", position="identity")
If you want to adjust the y-position of the labels, you can use the y=
aesthetic within stat_bin
: for example, y=..count..+1
will put the label one unit above the bar.
The above also works if you use geom_text
and stat="bin"
inside.
A hard way to do it. I'm sure there are better approaches.
ggplot(mtcars,aes(factor(cyl))) +
geom_bar() +
geom_text(aes(y=sapply(cyl,function(x) 1+table(cyl)[names(table(cyl))==x]),
label=sapply(cyl,function(x) table(cyl)[names(table(cyl))==x])))
When wanting to add different info the following works:
ggplot(mydata, aes(x=clusterSize, y=occurence)) +
geom_bar() + geom_text(aes(x=clusterSize, y=occurence, label = mydata$otherinfo))