具有个性化颜色的热图
Heat map with personalised colours
我正在寻找一种创建自定义热图的简单方法(在 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))
在 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)
在 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()
编辑:使用提供的数据(我将其导入对象 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))
要在 Tableau 中创建颜色图表:如果您想为每个排名使用单独的颜色,只需将 [Ranking] 维度拖动到颜色即可。如果您想要带状输出,您可以创建一个颜色键计算字段,首先通过创建一个新的计算字段为每个排名分配一个值,例如:
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
number bands 代替,但以这种方式编写可以清楚地了解正在发生的事情每个分数。
我正在寻找一种创建自定义热图的简单方法(在 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))
在 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)
在 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()
编辑:使用提供的数据(我将其导入对象 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))
要在 Tableau 中创建颜色图表:如果您想为每个排名使用单独的颜色,只需将 [Ranking] 维度拖动到颜色即可。如果您想要带状输出,您可以创建一个颜色键计算字段,首先通过创建一个新的计算字段为每个排名分配一个值,例如:
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
number bands 代替,但以这种方式编写可以清楚地了解正在发生的事情每个分数。