Plot graphical distribution using ggplot2 and ggmap

I am trying to plot my data with ggplot2 onto a map using ggmap

my script is

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)

library(ggmap)
map <- get_map("Marion Island",zoom=9)
dis_map <- ggmap(map)

dis_map +
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)

R returns error

Error: Don't know how to add o to a plot

I think the issue here is that dis_map is already a ggplot object and you're trying to add another with + ggplot(gdat). Try removing the ggplot(gdat) line and defining your aes() and data = arguments individually for each geom you add to the map layer.

Thank @paul I am trying to follow your suggestion

> map.marion <- get_map("Marion Island",zoom=9)
> dis_map <- ggmap(map.marion)
> dis_map + 
>   geom_point(data=gdat,aes(x = x, y = y, z = z, fill = z)) + 
>   geom_tile() + 
>   #coord_equal() +
>   geom_contour(color = "white", alpha = 0.5) +
>   geom_point(data = Space, mapping = aes(Longitude, Latitude),
>              shape=1, inherit.aes = FALSE)+
>   scale_fill_distiller(palette="Spectral", na.value="white",limits=c(0,0.4)) + 
>   #scale_x_reverse()+
>   theme_bw()+
>   ggtitle("Chlorophyll-a distribution")+
>   ylab("Latitude S") + xlab("Longitude E")+
>   labs(fill = "Chl-a (mg/m3)")

I think I am doing a mistake writing twice geom_point since R gives

Warning: Ignoring unknown aesthetics: z
Error: stat_contour requires the following missing aesthetics: z

How would you write it?

Hi Luca,

I think the error is coming from geom_contour not having any data or aes mapping assigned to it. Because there is no 'global' ggplot data + mappings defined in this case, you need to provide them for every geom. You can then provide the appropriate aes mapping individually for each geom. In this case it's telling you than geom_point does not recognize the z aesthetic, but geom_contour requires it. So your code should look something like this:

> map.marion <- get_map("Marion Island",zoom=9)
> dis_map <- ggmap(map.marion)
> dis_map + 
>   geom_point(data = gdat, aes(x = x, y = y)) + 
>   geom_tile(data = gdat, aes(x = x, y = y, z = z, fill = z)) + 
>   #coord_equal() +
>   geom_contour(data = gdat, aes(x = x, y = y, z = z, fill = z), 
       color = "white", alpha = 0.5) +
>   geom_point(data = Space, mapping = aes(Longitude, Latitude),
>              shape=1, inherit.aes = FALSE)+
>   scale_fill_distiller(palette="Spectral", na.value="white",limits=c(0,0.4)) + 
>   #scale_x_reverse()+
>   theme_bw()+
>   ggtitle("Chlorophyll-a distribution")+
>   ylab("Latitude S") + xlab("Longitude E")+
>   labs(fill = "Chl-a (mg/m3)")

let me know if that works!

Thank @paul it works, but the map hides my data plotted with ggplot.

this is what I obtained using your script

below it's what I would like to obtain (with the two islands of course)

Hi @LucaS. It's better not to cross-post both here and on StackOverflow.

I did not know it was not allowed

If you post your question in more than one place, you should, at the very least, always link to the other post. Not doing so is discouraged as someone might spend their time answering the question on StackOverflow, for example, when it’s already been answered here.

If you must ask in multiple places, please make it very clear that you have done so, and once you have an answer in one place, make sure to update all of your questions accordingly.

2 Likes