Updating RStudio ggplot2 gives me error

Hello Community,
I just update RStudio from version 0.99.896 to 1.1.383 (the latest) working on Ubuntu 16.4.

Before to update, I wrote a script of analysis in which I plot an interpolation of a data set using ggplot2.

this is the script:

require(akima) #this package interpolate values
require(ggplot2)
Space1 <- subset(x = ChlaMD0_3, Depth <= 14 & !(Station %in% c("P10","P11","P12c"))) 
Space2 <- subset(x = ChlaMD0_3, Depth <= 14 & Station %in% c("P10","P11") & Time == "D") 
Space <- rbind(Space1,Space2)
duplicated(Space)
attach(Space)

fld <- with(Space, interp(x = Longitud.E., y = Latitude.S., z = Chla2017_mean,duplicate = "mean"))
gdat <- interp2xyz(fld, data.frame=TRUE)

ggplot(gdat) + 
  aes(x = x, y = y, z = z, fill = z) + 
  geom_tile() + 
  #coord_equal() +
  geom_contour(color = "white", alpha = 0.5) + 
  scale_fill_distiller(palette="Spectral", na.value="white",limits=c(0,0.35)) + 
  scale_y_reverse()+
  theme_bw()+
  ggtitle("Chlorophyll-a (class size 0.3-2.7 um)")+
  ylab("Latitude S") + xlab("Longitude E")+
  labs(fill = "Chl-a (mg/m3)")+
  geom_point(data = Space, mapping =  aes(Longitud.E.,Latitude.S.),shape=1)

It worked really well, but after the updating I have this error massage:

Error in FUN(X[[i]], ...) : object 'z' not found

How can I solve the problem and plot the interpolation?

The error message is telling you that there is no z variable in one of the two datasets that you are using in your ggplot call. If the gdat <- interp2xys(...) is running without giving an error, then it is likely the geom_point(data = Space) giving you an issue. First try adding fill = NULL to your geom_point() function as it is likely trying to find the z variable you pass to your aes call and can not find it. The geom_point() line would look like this:

geom_point(data = Space, mapping =  aes(Longitud.E.,Latitude.S.),shape=1, fill = NULL)

If that does not work then your gdat data is likely not being calculated correctly.

1 Like

That aes call you have on the second line of the plotting code should either be inside the ggplot call or inside one of the geom_ calls. That could be causing you problems :slightly_smiling_face:

(If you put it inside ggplot(), it'll be inherited by all of the geoms unless they override it.)

Unfortunately, Both solutions do not work. R gives me the same error.
It is wired that now ggplot does not found the parameters x, y, z of gdat.

these are the structures of gdat and fld:

str(gdat)
'data.frame':	1600 obs. of  3 variables:
 $ x: num  37.3 37.3 37.4 37.4 37.4 ...
 $ y: num  45.7 45.7 45.7 45.7 45.7 ...
 $ z: num  NA NA NA NA NA NA NA NA NA NA ...
> str(fld)
List of 3
 $ x: num [1:40] 37.3 37.3 37.4 37.4 37.4 ...
 $ y: num [1:40] 45.7 45.8 45.8 45.9 45.9 ...
 $ z: num [1:40, 1:40] NA NA NA NA NA NA NA NA NA NA ...

Searching around into the web, I found the solution of the enigma.

I have to add the last line inherit.aes = FALSE, like this:
geom_point(data = Space, mapping = aes(Longitud.E., Latitude.S.),shape=1, inherit.aes = FALSE)

1 Like