我做了如下图表:
代码如下:
heatmap_branquesCAT <- ggplot(varIPI_branques_CAT_long, aes(x = time, y = branques, fill=values)) +
geom_tile(aes(width=31)) +
geom_text(aes(label = round(values, 1)), family="serif", color="black", size=4) +
scale_x_date(expand=c(0,0), breaks = "1 months", labels=date_format("%b %y")
# limits = c(as.Date("2023/01/01"), NA)) +
) +
scale_y_discrete(expand=c(0,0), labels=c("Subministrament d'aigua", "Indústries d'alimentació i begudes", "Altres indústries manufactureres",
"Materials i equips elèctrics, electrònics i òptics", "Energia elèctrica",
"Fabricació de productes farmacèutics", "Índex general (IPI)", "Indústries extractives",
"Indústries manufactureres", "Fabricació de maquinària i equips mecànics", "Metal·lúrgia i fabricació de productes metàl·lics",
"Fabricació d'altres productes minerals no metàl·lics", "Indústries del paper", "Fabricació productes cautxú i matèries plàstiques",
"Indústries químiques", "Indústries tèxtils",
"Fabricació de material de transport")) +
labs(title = "Índex de Producció Industrial",
subtitle = "% de variació interanual",
x = "",
y = " ",
caption = "Font: Idescat.") +
scale_fill_gradient2(
low = "#CC3300",
mid = "white",
high = "#66CC00",
# midpoint = 0,
limits = range(varIPI_branques_CAT_long$values)) +
guides(color=guide_legend(title="")) +
theme_minimal() +
tema +
theme(axis.text = element_text(size=12, colour = "black"),
legend.position="none")
heatmap_branquesCAT
我的数据样本:
structure(list(time = structure(c(19083, 19113, 19144, 19174,
19205, 19236, 19266, 19297, 19327, 19358, 19389, 19417, 19448,
18993, 19024, 19052, 19083, 19113, 19144, 19174, 19205, 19236,
19266, 19297, 19327, 19358, 19389, 19417, 19448, 18993, 19024
), class = "Date"), branques = c("Indústries extractives", "Indústries extractives",
"Indústries extractives", "Indústries extractives", "Indústries extractives",
"Indústries extractives", "Indústries extractives", "Indústries extractives",
"Indústries extractives", "Indústries extractives", "Indústries extractives",
"Indústries extractives", "Indústries extractives", "Indústries manufactureres",
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres",
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres",
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres",
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres",
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres",
"alimentació", "alimentació"), values = c(-17.2, -12, 0.6,
-6, 8.6, 7.5, 10, 37.1, 26.1, 28.3, 14, 16.8, -9.4, 0.8, 3.1,
2.5, -2.7, 6.4, 1.9, -2.9, 1.5, 2.5, 0.1, -1.9, -0.1, 5.3, -1.7,
1.8, -6.8, 4.6, 4.6), scaled_value = structure(c(-0.486164571662395,
-0.269301398928164, 0.256174750389396, -0.0190746611578974, 0.589810400749752,
0.543935498825203, 0.648196639562814, 1.77838740515852, 1.31963838591303,
1.41138818976213, 0.815014464742992, 0.931786942369116, -0.160869812561049,
0.0939546003127367, 0.338515461609155, 0.274716976053568, -0.278203232094857,
0.689407132164886, 0.21091849049798, -0.299469393946719, 0.168386166794255,
0.274716976053568, 0.019523033831218, -0.193138584687407, -0.00174312802064446,
0.572443241979642, -0.171872422835544, 0.200285409572049, -0.714159550058037,
0.59745871515621, 0.59745871515621), dim = c(31L, 1L))), row.names = 20:50, class = "data.frame")
所以问题是现在绿色是根据整体最大值(= 50,2)着色的。这个梯度应该根据每行的最大值和最小值来着色。
1条答案
按热度按时间cclgggtu1#
您需要创建一个新列,其中所有值都已缩放,并将填充值基于该列。(例如0到1之间,-1到1之间)
对于0-1,如下所示:
然后将ggplot代码中的
fill = value
替换为fill = scaled_value