1

I have a three column data that I want to plot as a 2D color map. The first column represents the x axis and the second represents the y axis. However the values in these are not in the form of a regular interval grid, with the data for the third column being available at rather random points in the 2D space. I have given a sample dataset below. The actual dataset is many thousands of lines long.

      X1        X2        X3
1  0.000000    NaN    1760
2  1.000000 0.0000000 1536
3  2.000000 0.0000000 1744
4  3.000000 0.0000000 1632
5  1.000000 1.5707963 1616
6  1.414214 0.7853982 1632
7  2.236068 0.4636476 1712
8  3.162278 0.3217506 1616
9  2.000000 1.5707963 1616
10 2.236068 1.1071487 1568
11 2.828427 0.7853982 1712
12 3.605551 0.5880026 1600
13 3.000000 1.5707963 1536
14 3.162278 1.2490458 1536
15 3.605551 0.9827937 1568
16 4.242641 0.7853982 1536

This data was obtained by actually "melting" (library reshape) a cartesian grid with values (raster), and then converting into polar coordinates, with an intent to plot the r and theta. The first column is r, the second is theta and the third is the intensity of the raster pixels. I want to be able to plot the values of the third column at the grid locations (on linear axes) as defined by the corresponding values of the first and second column. So, for example, taking the second row, the value of 1536 represented on a color scale at a location of (1,0)

I looked at a lot of options such as heatmaps, and found some help for matlab or python, but I want to be able to do this in R. Is there any way to accomplish this? Bonus if I am able to adjust the color scale etc.

To clarify, the first and second column have to be represented as linear axes itself (I explained the part about the r and theta to explain the origin of the data). I would hope the end result would be something like this: Raster plot

Hope it helps!

  • 15 points is pretty sparse... you could use `geom_tile` in `ggplot2`. There are examples in `?geom_raster` and `?geom_contour`, but I think your data is too sparse for the automatic methods. – Gregor Thomas Mar 07 '17 at 18:47
  • This answer may be of interest: http://stackoverflow.com/questions/19339296/plotting-contours-on-an-irregular-grid – bdemarest Mar 08 '17 at 02:23
  • @Gregor, I should have been more specific. This is a representative data. The actual dataset contains many thousands of lines like this. I have edited the question to reflect this. – Pradical2190 Mar 09 '17 at 14:50

1 Answers1

0

The solution below is taken directly from this fantastic answer Plotting contours on an irregular grid provided by @Henrik. It uses the akima package to interpolate z-values measured irregularly over 2D space. Then, geom_raster() is used to draw the heatmap.

dat = read.table(header=TRUE,
text="      X1        X2        X3
1  0.000000    NaN    1760
2  1.000000 0.0000000 1536
3  2.000000 0.0000000 1744
4  3.000000 0.0000000 1632
5  1.000000 1.5707963 1616
6  1.414214 0.7853982 1632
7  2.236068 0.4636476 1712
8  3.162278 0.3217506 1616
9  2.000000 1.5707963 1616
10 2.236068 1.1071487 1568
11 2.828427 0.7853982 1712
12 3.605551 0.5880026 1600
13 3.000000 1.5707963 1536
14 3.162278 1.2490458 1536
15 3.605551 0.9827937 1568
16 4.242641 0.7853982 1536")

library(akima)
library(reshape2)
library(ggplot2)

dat = dat[-1, ] # Remove row with NaN so `interp` will work properly.

# Interpolate 500 x 500 values, using spline interpolation.
res = interp(x=dat$X1, y=dat$X2, z=dat$X3, nx=500, ny=500, linear=FALSE)

# Reformat `interp` results into a long-format data.frame.
idat = melt(res$z, na.rm=TRUE)
names(idat) = c("x", "y", "X3")
idat$X1 = res$x[idat$x]
idat$X2 = res$y[idat$y]

p1 = ggplot(idat, aes(x=X1, y=X2, fill=X3)) +
     geom_raster() +
     scale_fill_gradientn(colours = terrain.colors(10))

ggsave("interpolated_heatmap.png", p1, height=4, width=6, dpi=150)

enter image description here

Community
  • 1
  • 1
bdemarest
  • 14,397
  • 3
  • 53
  • 56