I have a data frame with 3 columns. The first to columns determine the value of the third column and I would like to visualize this relationship through the usage of a heatmap. My plan was to initialize the data frame as a matrix with column 1 as the row names, column 2 as the column names and column three as the values of the matrix. Though, I am struggling to make this operation work. Would you be able to help me with this transformation, or is there a more efficient method?
Asked
Active
Viewed 158 times
0
-
1Please review how to create a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Please post your data (using `dput`). What code have you tried? What error messages are you getting? – Conor Neilson Mar 20 '20 at 11:54
1 Answers
0
You could try and interpolate between your values, and plot the resulting object:
# generate data
set.seed(42)
n <- 20
x <- runif(n, min = 10, max = 20)
y <- runif(n, min = 10, max = 20)
df <- data.frame(x = x, y = y)
df$z <- 2*x^2 + 0.1*y^3
# interpolate between data points
library(akima)
Field <- with(df, interp(x = x, y = y, z = z, duplicate = "strip"))
# plot
op <- par(mar = c(4,4,1,1))
image(Field)
points(y ~ x)
par(op)

Marc in the box
- 11,769
- 4
- 47
- 97
-
Thank you for your answer! While this seems to work for smaller amounts of data, I run into the following error once I use a greater number of rows: "Error in interp.old(x, y, z, xo, yo, ncp = 0, extrap = FALSE, duplicate = duplicate, : duplicate data points: need to set 'duplicate = ..'" I am quite unfamiliar with the process if interpolating and seem to miss the logic of it. Is there any recommandation you can give me? – Heiliger Jacobus Mar 20 '20 at 11:54
-
I think all you need to do is add `duplicate = "strip"` to your call to `interp`. I editted my answer. If you have duplicate combinations of x and y, this error get thrown. – Marc in the box Mar 20 '20 at 12:33