[英]Customize colors in ggplot heatmap using scale_colour_gradient
我正在嘗試使用ggplot2
和scale_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")
我的問題是:
low="gray"
和high="red"
但是我將元素設置為藍色范圍。 我該如何解決? 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.