plotting multiple images together using ggmap and plotRGB

I am trying to display one picture made of 2 plots, with on the left-side a plot given by:

plotRGB(myRasterBrick, b=1, g=2, r=3)

on the right-side a plot given by:

ggmap(Gmap)+geom_polygon(aes(x=x, y=y), data=df, alpha=.4)

Both images can be displayed perfectly well independently, but I cannot find a way to display them together, one left, one right.

To give you more detail, here is my full script:

For my first image:

#request a satellite image from Google
lat <- 1.266
lon <- 34.348
Gmap <- get_googlemap(center = c(lon = lon, lat = lat), zoom = 18, size = c(640, 640), scale = 2, format="png8",  maptype = c("satellite"))

#draw a square in the middle of the image
bbx <- c(left=lon-0.00005,bottom=lat-0.00005,right=lon+0.00005,top=lat+0.00005)
x <- c(bbx["left"], bbx["left"], bbx["right"], bbx["right"])
y <- c(bbx["bottom"], bbx["top"], bbx["top"], bbx["bottom"])
df <- data.frame(x, y)

#put the image and the square in one plot
a = ggmap(Gmap)+geom_polygon(aes(x=x, y=y), data=df, alpha=.4)

For my second image: cr1 is a geotiff made of 3 layers (RGB), downloaded from internet.

plotRGB(cr1, b=1, g=2, r=3)

Here is what I tried

par()

par(mfrow=c(1,2))
plot(a)
plotRGB(cr1, b=1, g=2, r=3)
par(mfrow = c(1, 1))

This shows plot(a) full screen and then another image with plotRGB(cr1, b=1, g=2, r=3) on a half screen.

grid.arrange()

library('gridExtra')
plot1 <- ggmap(Gmap)+geom_polygon(aes(x=x, y=y), data=df, alpha=.4)
plot2 <- plot(subset(cr1,1))
grid.arrange(plot1, plot2, ncol=2)

Which returns the following errors. I googled it, but couldn't find a proper solution.

Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, : only 'grobs' allowed in "gList"

链接地址: http://www.djcxy.com/p/30884.html

上一篇: ggplot图的网格页面pdf文件

下一篇: 使用ggmap和plotRGB将多个图像一起绘制