Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this short tutorial I will show how you can add the relative amount over the barplots, such that you have both, the absolute and relative Information in the plot. First, I create some artificial SNPs and TPMT-genotype.

set.seed(123)
library(data.table)
library(ggplot2)
library(reshape)
df <- data.frame(snp=factor(sample(3, 100, replace=T, p=c(0.25, 0.4, .35))),
tpmt=factor(sample(3, 100, replace=T, p=c(0.40, 0.55, .05))))
levels(df$snp) <- c("G/G", "A/G", "A/A") levels(df$tpmt) <- c("1/1", "1/3A", NA)


Now we calculate the percentage and reshape the data for ggplot.

df <- melt(df, id.vars="tpmt")
df <- data.table(df)
dt <- df[, table(value), by=tpmt]
dt$label <- df[, names(table(value)), by=tpmt]$V1
dt[, V1:=as.numeric(V1)]
dt[, tpmt:=as.character(tpmt)]
dt$tpmt[which(is.na(dt$tpmt))] <- "NA"
setkey(dt, tpmt)
dt["1/1", rel:=round(V1 / sum(V1) * 100, 0)]
dt["1/3A", rel:=round(V1 / sum(V1) * 100, 0)]
dt["NA", rel:=round(V1 / sum(V1) * 100, 0)]
dt[, rel:=paste(rel, "%", sep="")]


I used two times geom_bar as a hack for the legend. The text fileds are added by stat_identity. Experiment with it.

ggplot(dt, aes(x=tpmt, y=V1, fill=label)) +
geom_bar(position   = "dodge",
stat       = "identity") +
geom_bar(position   = "dodge",
stat       = "identity",
colour     = "white",
show_guide = FALSE) +
scale_fill_manual(values = c("#E69F00",
"#56B4E9",
"maroon"),
name = "") +
theme_classic() +
theme(legend.position="top") +
xlab("TPMT-genotype") +
ylab("Count") +
labs(title="Titel") +
stat_identity(aes(x=tpmt, label=rel),
geom="text",
position=dodgewidth,
vjust=-1) +
ylim(c(0, 40)) Update 18.04.2013 Here you get a better version.