Is there an R function to plot a single variable as a heatmap?

324 Views Asked by At

I've generated some RNA-seq data and have my list of DE genes between two groups and fold-changes (log2FC).

I'd like to be able to plot a summary heatmap of log2FC for this group comparison in a single bar similar to one in this figure

enter image description here.

Would anyone happen to know ideas on how I could put together a ggplot script for this?

Thank you.

1

There are 1 best solutions below

2
On

You can achieve something very similar using geom_tile() in ggplot.

library(ggplot2)
library(tidyr)
library(dplyr)

# generate random fold changes
df <- data.frame(
  group1 = rnorm(100),
  group2 = rnorm(100)
)
# get order by first group
df$pos <- rank(df$group1, ties.method="first")

# convert from wide to long for ggplot
df.long <- pivot_longer(
  df, c("group1","group2"),
  names_to="group", values_to="logFC"
)

ggplot(df.long, aes(x=pos, y=reorder(group, desc(group)), fill=logFC)) +
  geom_tile() +
  scale_fill_gradient2(low="blue", mid="black", high="yellow", midpoint=0) +
  theme_void() +
  theme(
    legend.position="top",
    axis.text.y=element_text()
  )

Produced plot:

enter image description here