branch.lizard
branch.lizard

Reputation: 595

Construct R heatmap from specified xy-coordinates and a value

I have a dataframe which contains the x coordinate, y coordinate, and a value at that position. I would like to create a heatmap (density plot?) from this data which is colored based on value at each position. I am looking for a heatmap like heatmap which came from this https://stackoverflow.com/questions/5004063/creating-a-heat-map-from-x-y-corrdinates-in-r. My data is different however. Rather than having a value for x and a value for y, I have coordinates and a value found at that pair. I also am needing the axes of the plot to be the x and y coordinates.

Here is an example of my data:

x_coord <- c(1,2,3,4)
y_coord <- c(1,2,3,4)
value <- c(12,15,19,30)
foo.df <- data.frame(x_coord, y_coord, value)

      x_coord y_coord value
 1       1       1    12
 2       2       2    15
 3       3       3    19
 4       4       4    30

If this is a repost, please link me and I will delete this post. I have found quite a bit on heatmaps for R, but none which seemed to work with data of this nature.

Any ideas?

Upvotes: 3

Views: 7164

Answers (2)

smci
smci

Reputation: 33970

I suspect you're looking for an image() plot, i.e. simple tiles on a 4x4 grid, rather than a continuous function/heatmap. Especially if your coordinates are in the range x=1..4,y=1..4 and you have all 16 complete values.

See image() in base, or geom_tile() in ggplot2 package; or qplot(..., geom='tile')

When you say "create a heatmap (density plot?) from this data which is colored based on value at each position" you simply mean "plot a tiled image".

Upvotes: 2

Math
Math

Reputation: 1294

Try this one :

x_coord <- c(1,2,3,4)
y_coord <- c(1,2,3,4)
value <- c(12,15,19,30)
foo <- data.frame(x_coord, y_coord, value)
library(MBA)
foo=foo[ order(foo[,1], foo[,2],foo[,3]), ]
mba.int <- mba.surf(foo, 300, 300, extend=T)$xyz.est
library(fields)
fields::image.plot(mba.int)

enter image description here

Upvotes: 3

Related Questions