[ggplot2] Examples for various ggplots that we use frequently
library(ggplot2);
library(reshape)
dat2 = as.data.frame(melt(dat))
dat2$text = signif(dat2$value,2)
p1=ggplot(dat2, aes(x=X2,y=X1, label=text)) +
geom_tile(aes(fill=-log10(value)),color="grey60") +
scale_fill_gradient(name = '-log10(P)', low = 'white', high = 'red') +
theme(axis.text.x=element_text(angle=50, size=10, hjust=1)) + ylab("") + xlab("") +
geom_text(size=3) + ggtitle("Cell-type Enrichment")
# hclust + heatmap
Seurat_Heatmap_By_Cluster_Hclust_Genes_SetColWidths <- function(
genes, clusterOrder) {
print(“Seurat_Heatmap_By_Cluster_Hclust_Genes_SetColWidths”)
# Subset expression matrix
exM <- centSO@scale.data
exM <- exM[row.names(exM) %in% genes, ]
# Obtain the dendrogram
dend <- as.dendrogram(hclust(d = dist(exM), method = "ward.D2"))
dend_data <- dendro_data(dend)
# Setup the data, so that the layout is inverted (this is more
# “clear” than simply using coord_flip())
segment_data <- with(
segment(dend_data),
data.frame(x = y, y = x, xend = yend, yend = xend))
# Use the dendrogram label data to position the gene labels
gene_pos_table <- with(
dend_data$labels,
data.frame(y_center = x, gene = as.character(label), height = 1))
# Limits for the vertical axes
gene_axis_limits <- with(
gene_pos_table,
c(min(y_center - 0.5 * height), max(y_center + 0.5 * height))) + 0.1 * c(-1, 1) # extra spacing: 0.1
# Facet dendrogram so it lines up with faceted heatmaps
segment_data$Facet <- “”
# Dendrogram plot
plt_dendr <- ggplot(segment_data) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
scale_x_reverse(expand = c(0, 0.5)) +
scale_y_continuous(breaks = gene_pos_table$y_center,
labels = gene_pos_table$gene,
limits = gene_axis_limits,
expand = c(0, 0)) +
facet_wrap(~Facet) +
labs(x = “Distance”, y = “”, colour = “”, size = “”) +
theme_bw() +
theme(panel.grid.minor = element_blank()) +
theme(strip.background = element_blank())
# Heatmap plot
geneGroupDF <- data.frame(GENE = gene_pos_table$gene, GROUP = “”)
gg <- Plot_Marker_Genes_Heatmap_SetColWidths(geneGroupDF = geneGroupDF)
# Combine heatmap and dendrogram
pg <- plot_grid(plotlist = list(gg_dendr, gg)
, ncol = 2
, rel_widths = c(0.2,1), align = ‘h’, axis = ‘t’)
return(pg)
}
title = "asdM12"
data = as.data.frame(t(M12_betas[,1:16])); ## Make data frame with Columns for Beta, SEM, Group
data$Col = rownames(data); #really should be called data$Row
data$Group = as.factor(substr(rownames(data),start = regexpr(".",rownames(data),fixed=T)+1,50)) # Make a factor variable for Group
bp = ggplot(data, aes(x=reorder(Col, as.numeric(Group)), y=Beta, fill=Group)) # Plots groups in alphabetical order
bp +
geom_bar(stat="identity") +
geom_errorbar(aes(ymin=(Beta - SEM), ymax=(Beta + SEM)), position=position_dodge(width=0.8), width=0.25,size=0.25) +
ggtitle(title) +
theme(plot.title = element_text(size=20, face="bold", vjust=2)) +
labs(x="", y="regression beta") +
theme(axis.text.x=element_text(angle=50, size=10, hjust=1)) + theme(axis.text.y=element_text(size=10, vjust=0.5)) +
theme(
legend.title = element_text(size=12),
plot.title = element_text(size=20, face="bold"),
axis.title.x = element_text(size=14, vjust=-0.35, face="bold"),
axis.title.y = element_text(size=14, vjust=0.5)
)
colors "#F8766D" "#00BA38" "# a"
"#F8766D" "#00BA38" "#619CFF"