-1

I am graphing a data set of many points in the x, y and z dimensions. For each x and y there can be multiple z's. What I am attempting to do is divide the xy-plane into a grid of 50x50 and then graph the maximum values of z for each box in a contour plot. I thought the code below that I found online worked but, upon closer inspection, it looks like it is dropping points or skewing the data. Is there something wrong with my code?

df<-eff[c("Xs",Ys","Zs")]
df$x<-cut(df$Xs, breaks=50, labels=FALSE)
df$y<-cut(df$Ys, breaks=50, labels=FALSE)
df.max<-expand.grid(x=1:50, y=1:50)
df.max<-merge(df.max, aggregate(Zs~x+y, df, max), all.x=TRUE)
z<-matrix(df.max$Zs, nr=50, nc=50)
x.values <- min(df$Xs)+(0:49)*diff(range(df$Xs))/50
y.values <- min(df$Ys)+(0:49)*diff(range(df$Ys))/50
image2D(x=x.values, y=y.values, z, rasterImage = TRUE, contour = list(lwd = 2, col = jet.col(11)),
    main="Contour Plot", xlab="Xs",ylab="Ys", clab="Zs", ylim=c(0,max(df$Ys)*1.05), 
    xlim=c(0,max(df$Xs)*1.05))

According to the plot of just the xy-plane (which I believe is correct), the profile of the contour plot should look like this:

enter image description here

Notice that it intersects the y-axis around 90k and there is a minimum around (25k,73k).

My contour plot from the same data looks like this: enter image description here

I do not have sample data to provide at the moment because my model is in the middle of running and I don't want to start it all over. I can add a sample later if necessary.

here is a link to the data

user3390169
  • 1,015
  • 1
  • 11
  • 34
  • Can somebody explain why this was voted down? I thought it was pretty clear but I would like to know for next time. – user3390169 Sep 10 '15 at 22:24
  • Probably because your code doesn't run if I just copy and paste into my R session. I don't know what `image2D` is, and you're missing an apostrophe in your initial statement. I've added a way you could get it working using `ggplot`, note that you could also use `akima` with `interp` function to do it too. – chappers Sep 11 '15 at 00:22

1 Answers1

1

Firstly, since you want to take the max, you probably should strip out all the useless information.

library(dplyr)
df <- df %>% group_by(Xs, Ys) %>%
  summarize(Zs = max(Zs))

From here you could use akima and simply run fld <- with(df, interp(x = Xs, y = Ys, z = Zs)) and follow this

However my R session kept crashing, so here is an approximation which sort of works:

round_lvl_X = floor(diff(range(df$Xs))/51)
round_lvl_Y = floor(diff(range(df$Ys))/51)
df_summary <- df %>% 
  mutate(
    x = round(Xs/round_lvl_X)*round_lvl_X,
    y = round(Ys/round_lvl_Y)*round_lvl_Y
  ) %>%
  group_by(x, y) %>%
  summarize(z=max(Zs))

library(ggplot2)
ggplot(data=df_summary, aes(x=x, y=y, z=z)) + 
  geom_tile(aes(fill=z)) + 
  stat_contour()

Which gives you the plot which "looks" like what you're after...

enter image description here

Community
  • 1
  • 1
chappers
  • 2,415
  • 14
  • 16