简体   繁体   中英

How to change the gradient scale in ggplot2?

I want to draw a heatmap using ggplot2 with the following code.

 ggplot() +
     geom_raster(data = df, aes(cytoband1_2, cytoband2_2, fill = SELECT2)) +
     scale_fill_gradientn('SELECT score', colours = colorRampPalette(c('#1F78B4', 'white', '#CC0C00'))(50), na.value = NA) +
     guides(x = guide_axis(angle = 90, n.dodge = 2, title = ''),
            y = guide_axis(n.dodge = 2, title = '')) +
     theme(panel.grid = element_blank(),
           panel.background = element_blank(),
           axis.ticks = element_blank())

在此处输入图片说明

I want the value of zero to be mapped to white of the gradient. I think a corrected color scale function is required. How to achieve it? Thank you.

(I want to put a data example to repeat the picture but I was warranted that my post was mostly code.)

Whithout a reproducible data is difficult. But try this code. Maybe works

  ggplot() +
  geom_tile(data = df, aes(cytoband1_2, cytoband2_2, fill = SELECT2)) +
  scale_fill_gradient(high = "red", mid = "white", low = "blue") +
  guides(x = guide_axis(angle = 90, n.dodge = 2, title = ''),
         y = guide_axis(n.dodge = 2, title = '')) +
  theme(panel.grid = element_blank(),
        panel.background = element_blank(),
        axis.ticks = element_blank())

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