I am trying to combine a ggplot result and a plotRGB of a spatial raster into one plot and write it to PNG. I have already found how to combine ggplots or do a multiplot but this specific setup I do not know how to get to work. This code is also integrated in a for-loop, which means that a print statement needs to be used to actually plot the ggplot (in either PNG or Rstudio).
library(raster)
library(ggplot2)
plotRGB(brick(raster(matrix(rnorm(10000),ncol=100)),
raster(matrix(rnorm(10000),ncol=100)),
raster(matrix(rnorm(10000),ncol=100))),r=1,g=2,b=3,stretch="lin")
ggdata=data.frame("x"=seq(100),"y"=rnorm(100))
ggplot(ggdata,aes(x=x,y=y))+
geom_line()
Does anyone of you know how to combine these two into a PNG (favorably shown in a for-loop, but not necessary)?