[英]R: Dividing heat map using colors with geom_tile() and scale_fill_gradientn()
I am producing many heat maps which should all have the same color scheme, as follows: 我正在制作许多热图,它们都应具有相同的配色方案,如下所示:
EDIT: It is important that each color gradients over their respective numbers. 编辑:重要的是每种颜色在其各自的数字上渐变。 Ie in Example A below, 18, 20, and 25 are the "darkest" colors on the map. 即,在下面的示例A中,18、20和25是地图上的“最暗”颜色。
My code is below: 我的代码如下:
library(ggplot2)
library(dplyr)
library(scales)
graphdata$pct <-ifelse(graphdata$A-graphdata$B>=0,graphdata$n/sum(graphdata$n),-graphdata$n/sum(graphdata$n))
graphdata$color_group <- ifelse(graphdata$A-graphdata$B<0,1,
ifelse(graphdata$A-graphdata$B>0,2,3))
graphdata$rescale <- 100*graphdata$pct + 100*(graphdata$color_group-1)
gradientends <-c(min(graphdata$rescale),
ifelse(dim(subset(graphdata,rescale<=0,select=c(rescale)))[1]==0,0,max(subset(graphdata,rescale<=0,select=c(rescale)))),
ifelse(dim(subset(graphdata,rescale>=100 & rescale<200,select=c(rescale)))[1]==0,0,min(subset(graphdata,rescale>=100 & rescale<200,select=c(rescale)))),
ifelse(dim(subset(graphdata,rescale>=100 & rescale<200,select=c(rescale)))[1]==0,0,max(subset(graphdata,rescale>=100 & rescale<200,select=c(rescale)))),
ifelse(dim(subset(graphdata,rescale>=200,select=c(rescale)))[1]==0,0,min(subset(graphdata,rescale>=200,select=c(rescale)))),
max(graphdata$rescale))
colorends <- c("tomato1","lightpink1","lightgoldenrod1","goldenrod1","lightsteelblue1","steelblue2")
ggplot(data = graphdata, aes(x = as.factor(A), y = as.factor(B))) +
geom_tile(aes(fill = rescale), colour = "white") +
scale_fill_gradientn(colors=colorends,values=rescale(gradientends)) +
theme(legend.position = "none",
axis.text=element_text(size=12,face="bold"),
panel.background = element_blank(),
axis.ticks=element_blank(),
plot.title = element_text(hjust=0.5)) +
geom_text(aes(label=n)) +labs(x="A",y="B")
My code works when there is data that can be bucketed in all three categories (Example A), but it fails to color correctly when there are 2 or less categories in the data (Example B). 当可以在所有三个类别中存储数据时,我的代码可以工作(示例A),但是当数据中包含2个或更少类别时,它无法正确着色(示例B)。
A <- c(1,2,3,1,2,3,2,1,3)
B <- c(1,2,3,2,3,2,1,3,1)
n <- c(10,5,20,15,18,10,10,10,25)
graphdata <- data.frame(A,B,n)
rm(A,B,n)
gives me what I'm expecting: 给我我所期望的:
A <- c(1,2,3)
B <- c(1,2,3)
n <- c(10,5,20)
graphdata <- data.frame(A,B,n)
rm(A,B,n)
does not give me a blue gradient: 没有给我蓝色渐变:
I would like Example B to look more like this: 我希望示例B看起来更像这样:
I would advise using alpha
to scale your colors. 我建议使用alpha
来缩放颜色。 This makes the problem much simpler: 这使问题变得更加简单:
cols <- c('-1' = "tomato1", '0' = "steelblue2", '1' = "goldenrod1")
ggplot(graphdata, aes(x = as.factor(A), y = as.factor(B))) +
geom_tile(aes(fill = as.factor(sign(A-B)), alpha = n), colour = "white") +
scale_fill_manual(values = cols) +
geom_text(aes(label = n)) +
labs(x = "A", y = "B") +
theme_minimal() +
theme(legend.position = 'none', panel.grid = element_blank())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.