Last active
August 25, 2017 00:20
-
-
Save clauda/b590ed76dd836ae24226084f3a1bb19e to your computer and use it in GitHub Desktop.
ggplot - dodge and stack bars combined - R Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(reshape2) | |
cancers <- c("BLCA", "BRCA", "COADREAD") | |
oncogenes <- c(18, 9, 4) | |
suppressors <- c(7, 8, 36) | |
proteins <- c(27, 22, 38) | |
drugs <- c(41, 33, 58) | |
dataset <- data.frame(Cancer = cancers, | |
Proteins = proteins, | |
Drugs = drugs, | |
Oncogenes = oncogenes, | |
Suppressors = suppressors) | |
mdata <- melt(dataset, "Cancer") | |
mdata$Cancers <- '' | |
mdata[mdata$variable == 'Proteins',]$Cancers <- "Proteins" | |
mdata[mdata$variable == 'Drugs',]$Cancers <- "Drugs" | |
mdata[mdata$variable == 'Oncogenes',]$Cancers <- "Genes" | |
mdata[mdata$variable == 'Suppresors',]$Cancers <- "Genes" | |
ggplot(mdata, aes(x = reorder(Cancers, value), y = value, fill = variable)) + | |
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Cancer) + | |
geom_text(aes(label=value), vjust = 0, color="white", | |
position = position_stack(vjust = 0.5), size = 2) + | |
scale_fill_brewer(palette="Paired") + theme_minimal() + | |
theme(axis.title.x=element_blank(), | |
axis.title.y=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank()) + labs(fill="Legend") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result:
