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

Here is the game version of my problem

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

I would then like to create a heat map z vs. (x + y) and (xy). The problem is that z is a vector and is not determined by all combinations of y and x. Please note: I am not looking for an answer that generates z for these missing values , which is not a reality in the real version of the problem. This is the minimum version to work. For all the solutions that I managed to find, such as filled.contour , I need a matrix for z defined by a grid of independent variables, and not just a set of data points (x, y, z) without any structure.

+6
source share
4 answers

The akima package has what you need. It performs two-dimensional interpolation using interp . It generates z values ​​for missing combinations, but can't you just exclude them if you want? If you are not generating z-values, just draw a 3d scatter z ~ x * y.

 x = runif(10); y = runif(10); z = (x+y)*(xy); library(akima) dens <- interp(x+y, xy, z, xo=seq(min(x+y), max(x+y), length=100), yo=seq(min(xy), max(xy), length=100), duplicate="median") filled.contour(dens, xlab="x+y", ylab="xy", main="z", color.palette = heat.colors) 

enter image description here

If you are really determined not to interpolate to add to the ggplot options provided by @Frank, there are many aesthetic elements that you can use to contrast points in the third dimension.

 library(ggplot2) dat <- data.frame(x1=x+y, x2=xy, 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

+7
source

Here is the opportunity with ggplot2 , where I do not generate values ​​for z when they are "missing", as opposed to the answer using interpolation.

 set.seed(54321) x = runif(10) y = runif(10) z = (x+y)*(xy) ggplot(df, aes(x+y, xy, fill=z)) + scale_fill_gradient(low = "blue", high = "red") + geom_tile() 

enter image description here

You can force / resize the tile and general appearance with round or cut :

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

enter image description hereenter image description here

+5
source

Perhaps the easiest way to achieve a result that looks like what, in my opinion, is the requested output, could be to use the lattice package:

 set.seed(12358) x <- runif(10) y <- runif(10) z <-(x+y)*(xy) x1<-x+y y1<-xy library(lattice) df<-data.frame(x=x1,y=y1,z=z) levelplot(z~x1*y1,df,cuts=9,col.regions=grey.colors(10)[10:1]) 

enter image description here

But admittedly, this does not look very pretty. Probably the best way to present the data is through an interactive 3D scatter chart, which can be generated with the rgl package, as shown below. For this diagram, I used a function from Winston Chang's "R Graphics Cookbook" to draw vertical blue lines:

 library(rgl) plot3d(x1,y1,z, size=1,type="s") interleave <- function(v1,v2) as.vector(rbind(v1,v2)) segments3d(interleave(x1,x1), interleave(y1,y1), interleave(z,0),alpha=0.4,col="blue") planes3d(a=0,b=0,c=1,d=0,alpha=0.1) 

enter image description here

Since the OP clearly stated that interpolation is not required, I refrain from publishing simple features for displaying such continuous heatmaps, although I believe that for this type of dataset it usually makes sense to interpolate the data.

In fact, I have some difficulties in understanding the use of a heat map (which, in my opinion, is a 2D object) applied to an unstructured set of unconnected points.

+3
source

Here is a basic chart option:

 x = runif(10); y = runif(10); z = (x+y)*(xy); n = 5 zz = cut(z, n) cols <- heat.colors(n) plot(x, y, col=cols[zz], cex=4, pch=20) legend('topright', legend=levels(zz), pch=20, col=cols, pt.cex=3) 
+3
source

All Articles