如何使用3个向量绘制轮廓/热图图?

Wet*_*ent 6 plot r

这是我的问题的玩具版本

x = runif(10);
y = runif(10);
z = (x+y)*(x-y);
Run Code Online (Sandbox Code Playgroud)

我想生成z与(x + y)和(xy)的热图.问题是z是一个向量,并没有在y和x的所有组合上定义.请注意我不是在寻找为这些缺失值生成z的答案,这在问题的真实版本中是不可能的.这只是一个可以使用的最小版本.所有的解决方案我已经能够找到,如filled.contour需要在自变量的网格中指定,而不仅仅是一组没有结构(X,Y,Z)的数据点Z A矩阵 本身.

pic*_*ick 7

包装满足akima您的需求.它用双变量插值interp.它确实为缺失的组合生成z值,但是如果你想要的话,你不能排除它们吗?如果您不生成z值,则只绘制z~x*y的三维散点.

x = runif(10);
y = runif(10);
z = (x+y)*(x-y);

library(akima)
dens <- interp(x+y, x-y, z, 
               xo=seq(min(x+y), max(x+y), length=100),
               yo=seq(min(x-y), max(x-y), length=100),
               duplicate="median")

filled.contour(dens, xlab="x+y", ylab="x-y", main="z",
               color.palette = heat.colors)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果您真的设置为不插值,要添加到@Frank提供的ggplot选项,您可以使用许多美学来将点对比第三维.

library(ggplot2)
dat <- data.frame(x1=x+y, x2=x-y, z=z)

## Scaling points by z dimension using size, color, and shading
ggplot(dat, aes(x1, x2, size=z, alpha=z, color=z)) +
  geom_point() +
  scale_color_gradient(low="red", high="yellow") +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Jot*_*ota 5

这是一种可能性,ggplot2当我们"丢失"时,我不会为z生成值,这与使用插值的答案不同.

set.seed(54321)
x = runif(10)
y = runif(10)
z = (x+y)*(x-y)

ggplot(df, aes(x+y, x-y, fill=z)) +
  scale_fill_gradient(low = "blue", high = "red") + geom_tile()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

您可以使用round或强制/操纵图块大小和整体外观cut:

ggplot(df, aes(round(x+y,1),round(x-y,1), fill=z)
  scale_fill_gradient(low = "blue", high = "red") + 
  geom_tile()
# OR
ggplot(df, aes(cut(x+y, 15), cut(x-y, 15), fill=z))
  scale_fill_gradient(low = "blue", high = "red") + 
  geom_tile() + 
  theme(axis.text.x=element_blank(), axis.text.y=element_blank())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述在此输入图像描述