0

I am trying to plot a heatmap (colored by odds ratios) using ggplot2. The odds ratio values range from 0-200. I would like my heatmap legend to show markings corresponding to certain values (0.1, 1, 10, 50, 100, 200). This is the code I am using but my legend does not label all the values (see below)

Code below:

map is a sample data frame with columns: segments, OR, tissue type

segments <- c("TssA", "TssBiv", "BivFlnk",  "EnhBiv","ReprPC",  "ReprPCWk", "Quies", "TssAFlnk", "TxFlnk", "Tx", "TxWk", "EnhG", "Enh", "ZNF/Rpts", "Het")
OR <- c(1.4787622, 46.99886002, 11.74417278, 4.49223136, 204.975818, 1.85228517, 0.85762414, 0.67926846, 0.33696213, 0.06532777, 0.10478027, 0.07462983, 0.06501252, 1.32922162, 0.32638438)
df <- data.frame(segments, OR)
map <- df %>% mutate(tissue = 'colon')


ggplot(map, aes(tissue,segments, fill = OR))+ geom_tile(colour="gray80")+
  theme_bw()+coord_equal()+
  scale_fill_gradientn(colours=c("lightskyblue1", "white","navajowhite","lightsalmon", "orangered2", "indianred1"),
                       values=rescale(c(0.1, 1, 10, 50, 100, 200)), guide="colorbar", breaks=c(0.1, 1, 10, 50, 150, 200))

enter image description here

I am looking for my legend to look something similar to this (using the values I specified): enter image description here

crazytaxi
  • 35
  • 5
  • 2
    It would be far more helpful if your question was reproducible. Without that information, all anyone can do is a best guess. You can try something like this to make your guide larger. `guides(fill = guide_colourbar(barwidth = 2, barheight = 10))` – Kat Nov 22 '22 at 00:40
  • Thanks! I've added some code to reproduce a sample dataset, hope that helps. I tried your suggestion but still did not get the result I wanted. – crazytaxi Nov 22 '22 at 02:07

1 Answers1

0

With your map data, first rescale OR to log(OR).
Also, you might want to assign white to OR = 1. If that's the case, your approach would be able to achieve that. You may want to try different limits values to achieve that with real data.

map_1 <-map %>% mutate(OR = log(OR))
OR_max <- max(map$OR, na.rm = TRUE)
log_list <- c(0.2, 1, 10, 50, 200) %>% log

ggplot(map_1, aes(tissue,segments, fill = OR))+ geom_tile(colour="gray80")+
    theme_bw()+coord_equal()+
    scale_fill_gradientn(
                         colours = c("red3", "white", "navy"), 
                         values=rescale(log_list), 
                         guide="colorbar", 
                         breaks=log_list, 
                         limits = c(1/OR_max, OR_max) %>% log, 
                         labels = c("0.1", "1", "10", "50", "200")
                         )

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27