下面的代码生成从左下角到右上角的热图
library(ggplot2)
library(reshape2)
set.seed(111)
n <- 10
m <- matrix(rnorm(n^2), n, n)
m <- cor(m)
m <- melt(m)
ggplot(m, aes(Var1, Var2, fill = value)) +
geom_tile()
Run Code Online (Sandbox Code Playgroud)
与@Axeman(但更有趣)相比,一个糟糕的解决方案是将旋转变换矩阵应用于数据。
为了了解我们需要什么样的变换,我仅在 3D 散点图上绘制了对角线(值=1)点。
绕 z(值)轴的旋转矩阵
包括添加的常数,最终方程为
可能有更好的方法来矢量化此转换,但这就是我的做法。
rot_m <- matrix(c(0,-1,0,1,0,0,0,0,1),3,3)
ftransform <- function(x){
t(rot_m %*% as.numeric(matrix(m[x,],3,1)) + matrix(c(0,11,0),3,1))
}
foo <- lapply(1:nrow(m),ftransform)
foo <- data.frame(do.call(rbind,foo))
names(foo) <- c("Var1","Var2","value")
ggplot(foo, aes(Var1,Var2,fill=value)) +
geom_tile()
Run Code Online (Sandbox Code Playgroud)
编辑:对奇怪的图像格式/布局表示歉意。