Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a contour/ heat map plot with 3 vectors?

Tags:

plot

r

Here is a toy version of my problem

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

I would like to then generate a heatmap of z vs. (x+y) and (x-y). The problem is that z is a vector and is not defined over all combinations of y and x. Please note I am not looking for an answer that generates z for these missing values, that is not a possibility in the real version of the problem. This is just a minimal version to work with. All solutions I've been able to find, such as filled.contour need a matrix for z specified over a grid of the independent variables rather than just a set of (x,y,z) data points with no structure per se.

like image 590
WetlabStudent Avatar asked Jul 04 '15 02:07

WetlabStudent


People also ask

How do you plot in 3D contour?

To plot 3D contour we will use countour3() to plot different types of 3D modules. Syntax: contour3(X,Y,Z): Specifies the x and y coordinates for the values in Z. contour3(Z): Creates a 3-D contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane.

Why do we plot heatmap?

By definition, Heat Maps are graphical representations of data that utilize color-coded systems. The primary purpose of Heat Maps is to better visualize the volume of locations/events within a dataset and assist in directing viewers towards areas on data visualizations that matter most.


1 Answers

Package akima has what you need. It does bivariate interpolation with interp. It does generate z values for the missing combinations, but couldnt you just exclude those if you wanted to? If you aren't generating z-values just plot a 3d scatter of 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)

enter image description here

If you are truly set on not interpolating, to add to the ggplot options provided by @Frank, there are a number of aesthetics you can use to contrast points by a third dimension.

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()

enter image description here

like image 112
Rorschach Avatar answered Nov 09 '22 21:11

Rorschach