使用 scale_colour_gradient 自定义 ggplot 热图中的颜色
Customize colors in ggplot heatmap using scale_colour_gradient
我正在尝试使用 ggplot2
和 scale_colour_gradient
绘制 Pearson 成对相关 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()
我正在尝试使用 ggplot2
和 scale_colour_gradient
绘制 Pearson 成对相关 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()