0

Hi Thank you for reading and potential help!

I am trying to annotate the x and y-axis of a corelation heatmap generated using ggplot with the following code:

# Gen ggplot
ggplot(cor.df, aes(x, y, fill = correlation)) +
  geom_tile(#color = "black") + theme_bw() + theme(axis.text.x = element_text(angle=70,vjust = 1, hjust=1)) + scale_fill_gradient2(low = "dodgerblue4", high = "red4", mid = "white", 
                                    midpoint = 0.5, limit = c(0,1), space = "Lab", 
                                    name="Pearson\nCorrelation")

Heatmap

The x and y-axis however are cells with a variety of metadata, such as origin, disease, celltype etc (for the sake of brevity say that these 3 metadata types exist).

cor.df above is a dataframe:

# Example dat:
                                    x                       y  correlation
1              cell1_disease1_origin1  cell1_disease1_origin1  0.5
2              cell2_disease1_origin1  cell2_disease1_origin1  0.5
3              cell3_disease1_origin2  cell3_disease1_origin2  0.5
4              cell4_disease2_origin2  cell4_disease2_origin2  0.5
5              cell5_disease2_origin3  cell5_disease2_origin3  0.5

The goal is to add some coding to the ggplot object so that annotation bars can be added on the x and y axis, for example what you observe on the y-axis here:

example heatmap

OR something like what is shown here:

heatmap with annotations on x and y axis

But instead of just one annotation, I want 3 annotation labels stacked on one another for the 3 types of metadata. I understand that metadata can be added to cor.df and then perhaps the link between metadata and plotting data can be used to A. Create the heatmap and then B the layers of annotation bars on the y and x axis?

I apologize in advance if this is not reprex enough.....feel free to destroy me if its not....thank you for the help!

P.S no not looking to use complexheatmap or heatmap, I want to learn solutions with native ggplot verbs because thats the best way honestly to learn and implement other ideas rather than forever navigate packages (which no doubt can be useful).

Thanks a million!!!

Already tried facet_wrap() and annotation that didnt work.....

1 Answer 1

0

Packages {cowplot} and {patchwork} are worth a try. For a quick fix, you can also roll your own helper geometries:

## Example data (pseudo correlation matrix in long format):
corr_mat <- 
  expand.grid(LETTERS[1:3], LETTERS[1:3]) |>
  cbind(cor = rnorm(9))
}
+ > corr_mat
  Var1 Var2          cor
1    A    A -0.267253341
2    B    A  0.640494120
3    C    A -0.430569260
## ...

one-off geometry:

geom_hlabel <- function(x = 0, xend = 0, y = 0, label = 'label'){
  list(
    geom_segment(aes(x = x, xend = xend,
                     y = y, yend = y), 
                 arrow = arrow(ends = 'both', length = unit(2, 'pt'))),
    geom_label(aes(x = x + abs(xend - x)/2, y = y, label = label))
  )
}

use in ggplot:

corr_mat |>
  ggplot() + 
  geom_tile(aes(Var1, Var2, fill = cor)) +
  geom_hlabel(x = .5, xend = 2.5, y = 0, label = 'range of origin 1') +
  geom_hlabel(x = 1.5, xend = 3.5, y = - .2 , label = 'disease XY here') +
  coord_cartesian(clip = 'off') ## turn clipping off!

homebrewn geom

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.