0

I am trying to do a countour plot with ggplot2. However, I kept get an error msg of "Not possible to generate contour data ".

A subset of the data is created for df.

x<-c(2.906633,28.056680,53.206720,78.356780,103.506900,128.656900)
y<-c(15.70863,15.72898,15.74938,15.76976,15.79016,15.81058)
z<-c(142.2,148.9,135.6,144.0,142.2,136.0)
df<-data.frame(x,y,z)

ggplot(df, aes(x, y, z = z))+stat_contour()

I was about to get the contour plot with example plot however. The difference of the volcano3d data is they are integer while df data is numeric.

library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")

# Basic plot
v <- ggplot(volcano3d, aes(x, y, z = z))
v + stat_contour()

A similar question has been asked before and solved with grid. My actual table is a data.frame from read.csv(), the x,y coordinate are from 70 measurement points within a +/-150 radius. How to create grid with this type of data.frame?

Find answers from using interp function from akima package: Is it possible to create a 3d contour plot without continuous data in R?

x <- ww5$x
y <- ww5$y
z <- ww5$CD

require(akima)

ww_map <- interp(x,y,z)

par(mar=c(5,5,1,1))
filled.contour(ww_map)

Now the remaining question is how to set x, y scale to +/-150? Is it possible to generate a circle around the contour with 150 radius?

2
  • That's not the real difference, the difference is that in volcano3d the z values are sampled on a grid of x and y values. This would work: x<-rep(c(2.906633,28.056680,53.206720), each=3); y<-rep(c(15.70863,15.72898,15.74938),3); z<-c(142.2,148.9,135.6,144.0,142.2,136.0, 54.343, 100.332, 30.234); df<-data.frame(x,y,z); ggplot(df, aes(x, y, z = z))+stat_contour()
    – MrFlick
    Nov 8, 2014 at 7:00
  • Thanks. So grid is needed for the stat_contour(). My actual table is a data.frame from read.csv(), the x,y coordinate are from measurement points in a +/-150 radius. How to create grid with this type of data.frame?
    – Rick
    Nov 8, 2014 at 16:00

0

Browse other questions tagged or ask your own question.