R: Barplot with absolute and relative values

[This article was first published on Statistik Stuttgart » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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))

tpmt

Update 18.04.2013 Here you get a better version.

To leave a comment for the author, please follow the link and comment on their blog: Statistik Stuttgart » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)