Skip to content

Instantly share code, notes, and snippets.

@clauda
Last active August 25, 2017 00:20
Show Gist options
  • Save clauda/b590ed76dd836ae24226084f3a1bb19e to your computer and use it in GitHub Desktop.
Save clauda/b590ed76dd836ae24226084f3a1bb19e to your computer and use it in GitHub Desktop.
ggplot - dodge and stack bars combined - R Language
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")
@clauda
Copy link
Author

clauda commented Aug 25, 2017

Result:
ggplot_sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment