简体   繁体   中英

Adjustments to heatmap (geom_tile)?

I'm very new to R and am trying to make a heatmap for a colleague of mine.

I've been able to generate a heatmap, but there are a few things wrong.

This is what my plot currently looks like:

热图

What I would like to change from my existing code:

  • I would like to figure out someway to order the sample names in my table so they're grouped together. For example, for the sample NC_rep1_nogel_wash10_IgG_p150 , I would like this to be grouped with all IgG samples.

In other words, I was thinking of something like this for ordering on the x and y axis, but not sure how to implement it:

    sample_order <-c(
"NC_rep1_nogel_wash10_IgG_p150",
"NC_rep2_nogel_wash10_IgG_p150",
"XL_rep2_nogel_wash10_IgG_p150",
"NC_rep1_nogel_wash10_p150",
"NC_rep2_nogel_wash10_p150",
"XL_rep2_nogel_wash10_p150"

    )
  • I'm also trying to change the dynamic range of colors. Right now it's hard to see the difference between 20% and 50% on the plot. Is it possible to invert the colors as well?

My code:

library(readxl)
library(tidyverse); library(scales)

best_peak <- read_csv("/Users/kylestangline/OneDrive - Eclipse Bioinnovations Inc/Projects/20210427-eclip-adar005/overlaps/clusters/heatmap.csv")


best_peak %>%
  separate(`Peak Comparison`, into = c("peak1", "peak2"), sep = " vs ") %>%
  mutate(label = paste(percent(Rate, accuracy = 1), comma(Intersection.Value, accuracy = 1), sep = "\n")) %>%
  ggplot(aes(peak1, peak2, fill = Rate)) +
  geom_tile() +
  geom_text(aes(label = label), size = 1.50, color = "gray80", lineheight = 0.7) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Table

One extra thing about the table: my code grabs the sample names from the Peak Comparison column, where it is separated by " vs "

Peak Comparison Param -a Param -b Intersection.Value Total -a Total -b Rate
NC_rep1_nogel_wash10_IgG_p150 vs NC_rep1_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_IgG_p150 12770 12770 12770 1
NC_rep1_nogel_wash10_IgG_p150 vs NC_rep1_nogel_wash10_p150 NC_rep1_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_p150 4370 12770 94156 0.342208301
NC_rep1_nogel_wash10_IgG_p150 vs NC_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_IgG_p150 2260 12770 10012 0.225729125
NC_rep1_nogel_wash10_IgG_p150 vs NC_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_p150 4034 12770 81936 0.315896633
NC_rep1_nogel_wash10_IgG_p150 vs XL_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_IgG_p150 2352 12770 11470 0.20505667
NC_rep1_nogel_wash10_IgG_p150 vs XL_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_p150 5358 12770 253054 0.419577134
NC_rep2_nogel_wash10_IgG_p150 vs NC_rep1_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_IgG_p150 2242 10012 12770 0.223931282
NC_rep2_nogel_wash10_IgG_p150 vs NC_rep1_nogel_wash10_p150 NC_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_p150 3220 10012 94156 0.321614063
NC_rep2_nogel_wash10_IgG_p150 vs NC_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_IgG_p150 10012 10012 10012 1
NC_rep2_nogel_wash10_IgG_p150 vs NC_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_p150 3046 10012 81936 0.304234918
NC_rep2_nogel_wash10_IgG_p150 vs XL_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_IgG_p150 1813 10012 11470 0.181082701
NC_rep2_nogel_wash10_IgG_p150 vs XL_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_p150 3850 10012 253054 0.384538554
XL_rep2_nogel_wash10_IgG_p150 vs NC_rep1_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_IgG_p150 2276 11470 12770 0.198430689
XL_rep2_nogel_wash10_IgG_p150 vs NC_rep1_nogel_wash10_p150 XL_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_p150 3463 11470 94156 0.301918047
XL_rep2_nogel_wash10_IgG_p150 vs NC_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_IgG_p150 1772 11470 10012 0.176987615
XL_rep2_nogel_wash10_IgG_p150 vs NC_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_p150 3219 11470 81936 0.280645161
XL_rep2_nogel_wash10_IgG_p150 vs XL_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_IgG_p150 11470 11470 11470 1
XL_rep2_nogel_wash10_IgG_p150 vs XL_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_p150 4439 11470 253054 0.38700959
NC_rep1_nogel_wash10_p150 vs NC_rep1_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_p150 NC_rep1_nogel_wash10_IgG_p150 4668 94156 12770 0.365544244
NC_rep1_nogel_wash10_p150 vs NC_rep1_nogel_wash10_p150 NC_rep1_nogel_wash10_p150 NC_rep1_nogel_wash10_p150 94156 94156 94156 1
NC_rep1_nogel_wash10_p150 vs NC_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_p150 NC_rep2_nogel_wash10_IgG_p150 3362 94156 10012 0.335797044
NC_rep1_nogel_wash10_p150 vs NC_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_p150 NC_rep2_nogel_wash10_p150 16636 94156 81936 0.203036516
NC_rep1_nogel_wash10_p150 vs XL_rep2_nogel_wash10_IgG_p150 NC_rep1_nogel_wash10_p150 XL_rep2_nogel_wash10_IgG_p150 3714 94156 11470 0.323801221
NC_rep1_nogel_wash10_p150 vs XL_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_p150 XL_rep2_nogel_wash10_p150 28631 94156 253054 0.304080462
NC_rep2_nogel_wash10_p150 vs NC_rep1_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_IgG_p150 4273 81936 12770 0.334612373
NC_rep2_nogel_wash10_p150 vs NC_rep1_nogel_wash10_p150 NC_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_p150 16572 81936 94156 0.202255419
NC_rep2_nogel_wash10_p150 vs NC_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_IgG_p150 3154 81936 10012 0.315021974
NC_rep2_nogel_wash10_p150 vs NC_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_p150 81936 81936 81936 1
NC_rep2_nogel_wash10_p150 vs XL_rep2_nogel_wash10_IgG_p150 NC_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_IgG_p150 3438 81936 11470 0.299738448
NC_rep2_nogel_wash10_p150 vs XL_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_p150 26501 81936 253054 0.323435364
XL_rep2_nogel_wash10_p150 vs NC_rep1_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_IgG_p150 5899 253054 12770 0.461942052
XL_rep2_nogel_wash10_p150 vs NC_rep1_nogel_wash10_p150 XL_rep2_nogel_wash10_p150 NC_rep1_nogel_wash10_p150 30344 253054 94156 0.322273673
XL_rep2_nogel_wash10_p150 vs NC_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_IgG_p150 4072 253054 10012 0.406711946
XL_rep2_nogel_wash10_p150 vs NC_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_p150 NC_rep2_nogel_wash10_p150 28397 253054 81936 0.346575376
XL_rep2_nogel_wash10_p150 vs XL_rep2_nogel_wash10_IgG_p150 XL_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_IgG_p150 4930 253054 11470 0.429816914
XL_rep2_nogel_wash10_p150 vs XL_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_p150 XL_rep2_nogel_wash10_p150 253054 253054 253054 1

If you convert your discrete x and y axis mappings to a factor you can control the order in which they appear.

sample_order <- c(
"NC_rep1_nogel_wash10_IgG_p150",
"NC_rep2_nogel_wash10_IgG_p150",
"XL_rep2_nogel_wash10_IgG_p150",
"NC_rep1_nogel_wash10_p150",
"NC_rep2_nogel_wash10_p150",
"XL_rep2_nogel_wash10_p150" )

best_peak %>%
  separate(`Peak Comparison`, into = c("peak1", "peak2"), sep = " vs ") %>%
  mutate(
   label = paste(percent(Rate, accuracy = 1),
                 comma(Intersection.Value, accuracy = 1), sep = "\n"),
   across(c(peak1,peak2),
          ~factor(., levels = sample_order))
    ) %>%
  ggplot(aes(peak1, peak2, fill = Rate)) +
  geom_tile() +
  geom_text(aes(label = label), size = 1.50, color = "gray80", lineheight = 0.7) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM