假设我有两个相同长度的矢量,一个用颜色填充,另一个用字母填充。
colors <- c("red","white","blue","green","yellow")
letters <- c("A","B","C","D",E")
创建排序热图的最佳方向是什么,两行,第一行包含每个字母,下一行是用数字对应的矢量索引着色的框?
-----------
|A|B|C|D|E|
-----------
|R|W|B|G|Y| #<---- Colored boxes, not just characters.
-----------
我想用ggplot输出这个图,但与ggplot中的热图不同,对于许多不同的变量会有许多不同的颜色。
答案 0 :(得分:2)
我在ggplot2中这样做。
首先,您需要将颜色和字母组成一个因子,以便保留相关的订单。
com:android:support:percent:23.0.1
然后您创建自己的填充调色板。和你的颜色一样。
colors <- c("red","white","blue","green","yellow")
letters <- c("A","B","C","D","E")
colors <- factor(colors, levels = colors)
letters <- factor(letters, levels = letters)
现在使用my_fill <- c("red", "white", "blue", "green", "yellow")
制作热图,geom_raster
放置字母并添加自己的填充比例。
geom_text
这是你想要的吗?重复颜色需要稍微调整一下。
答案 1 :(得分:1)
同样在ggplot中:
library(ggplot2)
#make data
colors = c("red","white","blue","green","yellow")
names(colors) <- c("A","B","C","D","E")
data <- expand.grid(letters = c("A","B","C","D","E"),y=c(1:2),
stringsAsFactors = F)
data$colour <- ifelse(data$y==1,colors[data$letters],"white")
data$label <- ifelse(data$y==1,"",data$letters)
#plot
p1 <- ggplot(data, aes(x=letters,y=y)) + geom_tile(aes(fill=colour),colour="black")+
scale_fill_identity() + coord_fixed()
p1 + geom_text(aes(label=label)) + theme_minimal() +
theme(
axis.text=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank()
) + labs(x="",y="")