2017-09-22 30 views
-1

我正在寻找一种简单的方法来创建自定义热图(使用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)) 
+1

到目前为止你做了什么?您如何创建没有指定颜色的热图的示例有助于了解您需要帮助的位置。 – asongtoruin

回答

0

在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

编辑:用提供的数据(I其导入到对象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

+0

谢谢,这实际上是我正在寻找的着色类型! – Bet

+0

我添加了我的数据的图片。你能解释我如何绘制他们,让我获得相同的结果你的照片? – Bet

+0

@请使用'dput'或至少以文本表的形式提供数据。 – missuse

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)) 

这微不足道barplot显示结果

library(ggplot2) 

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

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

enter image description here

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数字乐段代替,但是用这种方式编写它可以清楚地说明每个乐谱发生了什么。

+0

案例陈述不太详细 –