I am trying to get multiple heatmaps using facet_wrap and geom_tile function, I have 1000 samples.
My objective is to obtain a heatmap for each Parent_Gene that include the "Condition" in the “X” axis, "response value" as a “fill” and the Genes belonging to the same Parent_Gene in the “Y” axis.
ID | sl | condition | Gene | Response_Value | Parent_Gene |
---|---|---|---|---|---|
1 | S1 | disease1 | GeneKPN1 | 2.749526 | GeneKPN |
2 | S2 | disease2 | GeneKPN2 | 6.606618 | GeneKPN |
3 | S3 | disease1 | GeneBKJH1 | 4.697644 | GeneBKJH |
Here is my script so far. My issue is that I just want the genes belonging to the corresponding parent_gene depicted in each heatmap and not all the genes every time.
my_data <- read.csv("my_data.csv")
my_data$Parent_Gene <- gsub("([A-Z)]+)\\d+.*","\\1", my_data $Gene)
library(tidyverse)
gg<- ggplot(my_data, aes(x=Condition, y=Gene, fill= Response_Value))+
geom_tile(color="white", size=0.1)+
scale_fill_viridis(name="response levels")
gg + coord_equal()
gg + facet_wrap(~Parent_Gene, ncol=2)
gg <- gg + labs(x=NULL, y=NULL, title="Comparative response levels")
You need to add scales = "free"
to your facet_wrap()
. This will "free" up the scales, which in your case will leave only the included genes in your plot. You can also specify to free up individual scales, e.g. using free_x
or free_y
, which can be useful if you want to have different genes on the x-axis, but retain the scale of the y-axis.
These are also detailed in the help page, which you can access using ?facet_wrap
.