Last active
October 3, 2022 11:54
-
-
Save jokergoo/04830f0943009468dcee186540612b8e to your computer and use it in GitHub Desktop.
a simple 3D heatmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rect3D = function(x, y, w, h, l, theta = 60, default.units = "npc", fill = "white", col = "black") { | |
if(!is.unit(x)) x = unit(x, default.units) | |
if(!is.unit(y)) y = unit(y, default.units) | |
if(!is.unit(w)) w = unit(w, default.units) | |
if(!is.unit(h)) h = unit(h, default.units) | |
if(!is.unit(l)) l = unit(l, default.units) | |
x1 = x - w*0.5 | |
x2 = x + w*0.5 | |
y1 = y - h*0.5 | |
y2 = y + h*0.5 | |
xu = x + l * cos(theta/180*pi) | |
yu = y + l * sin(theta/180*pi) | |
a1 = xu - w*0.5 | |
a2 = xu + w*0.5 | |
b1 = yu - h*0.5 | |
b2 = yu + h*0.5 | |
fill = add_luminance(fill) | |
# front | |
grid.polygon(unit.c(x1, a1, a2, x2), unit.c(y1, b1, b1, y1), gp = gpar(fill = fill[2], col = col)) | |
# left | |
grid.polygon(unit.c(x1, x1, a1, a1), unit.c(y1, y2, b2, b1), gp = gpar(fill = fill[1], col = col)) | |
# top | |
grid.polygon(unit.c(a1, a1, a2, a2), unit.c(b1, b2, b2, b1), gp = gpar(fill = fill[3], col = col)) | |
} | |
add_luminance = function(col) { | |
rgb = col2rgb(col) | |
r = rgb[1, ]/255 | |
g = rgb[2, ]/255 | |
b = rgb[3, ]/255 | |
col = as(RGB(r, g, b), "polarLUV") | |
value = coords(col) | |
sequential_hcl( | |
n = 9, | |
h = value[, "H"], | |
c = value[, "C"], | |
l = c(30, 90) | |
) | |
} | |
# grid.newpage() | |
# rect3D(0.5, 0.5, 0.3, 0.3, 0.2, fill = "red") | |
m = matrix(sample(100, 36), 6) | |
ht = Heatmap(m, rect_gp = gpar(type = "none"), | |
layer_fun = function(j, i, x, y, w, h, f) { | |
v = pindex(m, i, j) | |
od = rank(order(-as.numeric(y), -as.numeric(x))) | |
for(k in od) { | |
grid.rect(x[k], y[k], w[k], h[k], gp = gpar(col = "white", fill = "#EEEEEE")) | |
} | |
for(k in od) { | |
rect3D(x[k], y[k], w[k]*0.6, h[k]*0.6, unit(v[k]/100, "cm"), fill = f[k]) | |
} | |
}, | |
cluster_rows = FALSE, | |
cluster_columns = FALSE, | |
show_heatmap_legend = FALSE | |
) | |
draw(ht, padding = unit(rep(10, 4), "mm")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment