热图与个性化的颜色

时间:2017-09-22 10:45:34

标签: python r tableau heatmap

我正在寻找一种简单的方法来创建自定义热图(在Python,或R或Tableau中)。我无法弄清楚如何根据需要个性化颜色。

基本上,我有一个带有功能和排名的.tsv文件。例如,排名在同一文件中从1到10,从-1到-10。

我需要为零填充白色。然后,较暗的颜色为1和-1然后变得更亮。所以,例如,我需要暗红色为1,浅红色为10,然后深蓝色为-1,浅蓝色为-10。

有关如何获得此结果的任何想法?

编辑: 这就是我的数据外观:

structure(list(Features = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 11L, 12L, 9L, 10L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L), .Label = c("char_per_tok", "cpos_dist_AUX", "cpos_dist_NUM", 
"dep_dist_aux", "dep_dist_nummod", "dep_freq_aux", "dep_freq_nmod", 
"dep_freq_nummod", "in_dict", "in_dict_types", "in_FO", "in_FO_types", 
"itwac_forme", "itwac_lemmi", "n_prepositional_chains", "prep_dist_3", 
"prep_freq_1", "prep_freq_3", "subj_post", "verb_edges_dist_7", 
"verb_edges_freq_7"), class = "factor"), A10 = c(1L, -14L, -6L, 
-8L, -5L, -7L, 3L, -3L, -1L, -11L, -2L, -4L, 0L, 59L, 4L, -9L, 
2L, -10L, 0L, -13L, -12L), A11 = c(3L, -14L, -6L, -8L, -5L, -7L, 
4L, -4L, -1L, -11L, -2L, -3L, 1L, 2L, 0L, -9L, 5L, -10L, 0L, 
-13L, -12L), A12 = c(3L, 0L, -3L, -5L, -2L, -4L, 0L, -1L, 0L, 
0L, 0L, 0L, 1L, 2L, 0L, -6L, 0L, -7L, 0L, -9L, -8L), A13 = c(3L, 
0L, -3L, 0L, -2L, 0L, 0L, -1L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, -4L, 
0L, -5L, 0L, 0L, 0L), A14 = c(1L, 0L, -3L, 0L, -2L, 0L, 0L, -1L, 
0L, 0L, 0L, 0L, 0L, 2L, 0L, -4L, 0L, -5L, 0L, 0L, 0L), A15 = c(2L, 
0L, -3L, 0L, -2L, 0L, 0L, -1L, 0L, 0L, 0L, 0L, 1L, 3L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L), A16 = c(0L, 0L, -4L, -5L, -1L, 0L, 0L, -2L, 
0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, -3L, 0L, 0L)), .Names = c("Features", 
"A10", "A11", "A12", "A13", "A14", "A15", "A16"), class = "data.frame", row.names = c(NA, 
-21L))

3 个答案:

答案 0 :(得分:1)

在R中,您可以使用ggplot2库,geom_tile来指定要绘制的内容,使用scale_fill_gradientn来指定颜色。这是一个例子:

#diamonds + column rank with a range of -10:10
library(ggplot2)
data(diamonds)
diamonds_1= data.frame(diamonds, rank = sample(c(-10:10), nrow(diamonds), replace = T))


ggplot(data = diamonds_1)+
  geom_tile(aes(color, cut, fill = rank))+
  scale_fill_gradientn(colors = c("lightblue", "blue", "white", "red", "pink"),
                       values = scales::rescale(c(-10, -1, 0, 1, 10)))+
  coord_equal()

enter image description here

编辑:提供数据(我将其导入对象z

z_melt = reshape2::melt(z, id.vars = 1 ) #convert to long format
library(ggplot2)
ggplot(data = z_melt)+
  geom_tile(aes(y = Features,  x = variable, fill = value))+
  scale_fill_gradientn(colors = c("#ccccff", "lightblue", "blue", "white", "red", "#ff7f7f", "#ffcccc"),
                       values = scales::rescale(c(min(z_melt$value), -10, -1, 0, 1, 10, max(z_melt$value))),
                       breaks = c(-10, 0, 10, 40),
                       labels=c(-10, 0, 10, 40))+                           
  coord_equal()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

enter image description here

答案 1 :(得分:0)

在R中,您可以使用scales包来生成颜色值。

要生成颜色,请使用gradient_n_pal。为负值选择所需的颜色,为正值选择颜色,然后在它们之间放置"white"seq(0, 1, length.out = 21)创建一个长度为21的向量,用于设置淡入淡出。

gradient <- scales::gradient_n_pal(c("purple", "white", "green"))(seq(0, 1, length.out = 21))

这个琐碎的条形图显示了结果

library(ggplot2)

Dframe <- data.frame(x = factor(-10:10)) 

ggplot(data = Dframe,
       mapping = aes(x = x)) + 
  geom_bar(fill = gradient)

enter image description here

答案 2 :(得分:0)

要在 Tableau 中创建颜色图表:如果您想要每个等级的单独颜色,只需将[排名]尺寸拖动到颜色即可。如果你想要一个带状输出你可以创建一个颜色键计算字段,首先通过创建一个新的计算字段为每个排名分配一个值,如:

If [Ranking] = -10 then "Cold"
ElseIf [Ranking] = -9 then "Cold"
ElseIf [Ranking] = -8 then "Cold"
ElseIf [Ranking] = -7 then "Cold"
ElseIf [Ranking] = -6 then "Cold"
ElseIf [Ranking] = -5 then "Warm"
ElseIf [Ranking] = -4 then "Warm"
ElseIf [Ranking] = -3 then "Warm"
ElseIf [Ranking] = -2 then "Warm"
ElseIf [Ranking] = -1 then "Warm"
ElseIf [Ranking] = -0 then "Warm"
ElseIf [Ranking] = 1 then "Warm"
ElseIf [Ranking] = 2 then "Warm"
ElseIf [Ranking] = 3 then "Warm"
ElseIf [Ranking] = 4 then "Warm"
ElseIf [Ranking] = 5 then "Hot"
ElseIf [Ranking] = 6 then "Hot"
ElseIf [Ranking] = 7 then "Hot"
ElseIf [Ranking] = 8 then "Hot"
ElseIf [Ranking] = 9 then "Hot"
ElseIf [Ranking] = 10 then "Hot"
else "Unknown ranking"  end

将此字段拖动到颜色,您可以将所选的味觉应用于它。

这是一个稍微冗长的编写计算字段的方式,根据您的排名字段的格式,您可以使用between数字段,但以这种方式编写它可以清楚地表明每个得分是怎么回事