Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a filled contour plot using data in lists

I have a data set that consists of 3 columns in a .csv file. The first 2 columns are map co-ordinates and the third is the percentage of zinc found in a borehole at the corresponding map co-ordinates. I would like to create a contour map to show the Zn concentration changes with distance. All of the examples of code I have been able to find use data in the form of a matrix, whereas mine are in lists. I have tried several different ways of plotting this which I have shown underneath, the majority of methods give me error messages along the lines of "object x not found" which I think is to do with the layout of my data. Does anyone know how to do this? I have added a similar data set to mine below. Thanks for any help in advance. Holly

Data set:

Statsrep <- structure(list(X = c(156000L, 156010L, 156421L, 156450L, 156500L, 156700L, 158420L, 158646L, 158970L, 159050L, 159050L, 159130L, 159155L), Y = c(143630, 143980, 147260, 145000, 146000, 142800, 146700, 145207, 147170, 145200, 144800, 147815, 145890), Zn = c(2, 8, 4, 0, 3, 0, 2, 7, 12, 0, 4, 19, 0)), .Names = c("X", "Y", "Zn"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L), class = "data.frame")
Statsrep

Code:

library(ggplot2)
Grade <- read.csv(file="filename.csv", header=TRUE, sep=",")
ggplot(Grade, aes(x$x="X", y$y="Y", z$z="Zn")) +
stat_contour()

library(lattice)
Grade <- read.csv(file="filename.csv", header=TRUE, sep=",")
levelplot(Grade ~x*y, data = Zn,
xlab = "Eastings", ylab = "Northings",
col.regions = terrain.colours)

Grade <- read.csv(file="filename.csv", header=TRUE, sep=",")
x$x <- X
y$y <- Y
z$z <- Zn
filled.contour(x$x, y$y, z$z, color = terrain.colours,
xlab = "Eastings", ylab = "Northings"),
plot.axes = {axis(1, seq(156000, 165000, by=1000)); axis(2, seq(142000, 150000, by=1000))},
key.title = title(main="Zn content\n(percent)"),
key.axes= axis(4, seq(0, 20, by = 2)))
like image 831
Holly Elliott Avatar asked Sep 26 '12 12:09

Holly Elliott


1 Answers

Working with ggplot2, you can create a contour plot with your example data set using:

ggplot(Statsrep, aes(x=X, y=Y, z=Zn)) + 
    geom_density2d()

to give

enter image description here

You had a couple of problems with the ggplot2 code. In particular, where you set the aesthetics you had:

aes(x$x="X", y$y="Y", z$z="Zn")
like image 178
csgillespie Avatar answered Sep 23 '22 05:09

csgillespie