簡體   English   中英

使用scale_colour_gradient自定義ggplot熱圖中的顏色

[英]Customize colors in ggplot heatmap using scale_colour_gradient

我正在嘗試使用ggplot2scale_colour_gradient繪制皮爾遜成對相關heatmap

這是我的示例數據:

library(dplyr)
library(ggplot2)
set.seed(1)

pairs.mat <- t(combn(1:5,2))
df <- data.frame(sample1=pairs.mat[,1],sample2=pairs.mat[,2]) %>% dplyr::mutate(association=runif(10,0.85,1))

這是我正在嘗試的ggplot2代碼:

heatmap.ggplot <- ggplot(df,aes(sample1,sample2,fill=association))+geom_tile(color="white")+
  scale_colour_gradient(low="gray",high="red",limit=c(min(df$association),1),space="Lab",guide="colourbar")+theme_minimal()+
  theme(axis.title.x=element_blank(),axis.title.y=element_blank(),axis.text.x=element_text(angle=45,vjust=1,size=12,hjust=1))+coord_fixed()+coord_flip()+labs(colors="Cor")

產生: 在此處輸入圖片說明

我的問題是:

  1. 我將范圍指定為low="gray"high="red"但是我將元素設置為藍色范圍。 我該如何解決?
  2. 我似乎無法使用labs(colors="Cor")更改圖例標題。 有什么想法嗎?

謝謝

您需要scale_fill_gradient

ggplot(df, aes(sample1, sample2, fill = association)) +
  geom_tile(color = "white") +
  scale_fill_gradient(
    name = "Cor", # changes legend title
    low = "gray",
    high = "red",
    limit = c(min(df$association), 1),
    space = "Lab",
    guide = "colourbar"
  ) + theme_minimal() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text.x = element_text(
      angle = 45,
      vjust = 1,
      size = 12,
      hjust = 1
    )
  ) + 
coord_fixed() + 
coord_flip() 

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM