1. Create a heatmap using ggplot2 that is faceted by tissue
type.
# load the necessary packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
# download the assignment data
expression <- read.csv("https://madisonherrboldt.github.io/HerrboldtBIOL7263/My_Lesson_Heatmaps/MAH_assignment_data.csv")
# view the columns and data
glimpse(expression)
## Rows: 696
## Columns: 4
## $ Sample <chr> "A.tri_mg", "A.tri_mg", "A.tri_mg", "A.tri_mg", "A.tri_mg",…
## $ Tissue <chr> "Mental Gland", "Mental Gland", "Mental Gland", "Mental Gla…
## $ Gene <chr> "PMF1", "PMF2", "PMF3", "PMF4", "PMF5", "PMF6", "PMF7", "PM…
## $ Expression <dbl> 0.00, 0.00, 1.10, 1.61, 0.00, 2.20, 0.00, 0.00, 1.22, 0.00,…
unique(expression$Tissue)
## [1] "Mental Gland" "Caudal Gland" "Cloaca" "Testes"
# using the code from the in-class demonstration, create a heatmap
ggplot(expression, aes(x = Sample, y = Gene, fill = Expression)) +
geom_tile(colour="black", linewidth=0.5)+
scale_fill_gradient(low = "black", high = "green")+
theme_grey(base_size=12)+
facet_grid(~ Tissue, switch = "x", scales = "free_x", space = "free_x") + #switch moves the facet labels to the bottom
#applying "free_x" removes the columns for which there is no data and
#applying "free_x" to space makes the columns the same width
ggtitle(label = "Pheromone Gene Expression") +
scale_x_discrete(labels=c(unique(expression$Sample)))+
theme(plot.title = element_text(face="bold"),
strip.placement = "outside", #this places the facet label outside the x axis labels
strip.text = element_text(face = "bold"), #this bolds the text in the facet label
legend.title = element_text(face = "bold"),
axis.title = element_text(face="bold"),
axis.title.x = element_blank(),
axis.text.y =element_text(color = "black"),
axis.text.x =element_text(color = "black"),
axis.ticks=element_blank(),
plot.background=element_blank(),
panel.background = element_blank(),
panel.border = element_blank())
## Warning: Ignoring unknown parameters: linewidth
2. Customize the heatmap with a title, color, and correct axis
labels/titles that are legible.
# using the code from the in-class demonstration, customize the heatmap
ggplot(expression, aes(x = Sample, y = Gene, fill = Expression)) +
geom_tile(colour="black", linewidth=0.5)+
scale_fill_gradient(low = "black", high = "purple")+
theme_grey(base_size=12)+
facet_grid(~ Tissue, switch = "x", scales = "free_x", space = "free_x") + #switch moves the facet labels to the bottom
#applying "free_x" removes the columns for which there is no data and
#applying "free_x" to space makes the columns the same width
ggtitle(label = "Pheromone Gene Expression") +
scale_x_discrete(labels=c(unique(expression$Sample)))+
theme(plot.title = element_text(face="bold"),
strip.placement = "outside", #this places the facet label outside the x axis labels
strip.text = element_text(face = "bold"), #this bolds the text in the facet label
legend.title = element_text(face = "bold"),
axis.title = element_text(face="bold"),
axis.title.x = element_blank(),
axis.text.y = element_text(color = "black"),
axis.text.x =element_text(angle = 270, hjust = 0, color = "black"),
axis.ticks = element_blank(),
plot.background = element_blank(),
panel.background = element_blank(),
panel.border = element_blank())
## Warning: Ignoring unknown parameters: linewidth