I'm trying to create a map which includes a raster and a polygon shapefile. Since I would also like to add a legend I'm working with the layout()
command. However, I found out that plotting a raster does overwrite the layout()
so it is recommended to use image()
instead of plot()
for the Raster ("R - plotting multiple rasters using matrix layout". It works totally fine with the layout()
command and the overlaying of the polygon shapefile is no problem either. But, the map is totally distorted, there are compressions in y
and y
direction (see Image). On the left the layers are plotted proportional, on the right the map is distorted:
Here is part of my code:
#loading shapes and raster datasets
CityMap <- raster("CityMap.tif")
RISE <- readShapeSpatial("RISE")
setwd("G:/maps")
pdf(paste("map",i,".pdf", sep=""), width=16.53, height=11.69)
#set the layout for the map
mat <- matrix(c(1,2), 1,2)
layout(mat, c(0.75, 0.25))
par(mar=c(0.5,0.5,2.5,0.5), oma=c(0,3,1,0))
#set map extent
maxxlim <- max((data$UTM_x) + 800)
maxylim <- max((data$UTM_y) + 800)
minxlim <- min((data$UTM_x) - 800)
minylim <- min((data$UTM_y) - 800)
#plot Raster
image(Stadtkarte, xlim=c(minxlim, maxxlim), ylim=c(minylim, maxylim), maxpixels = 3000000, axes=FALSE, col = gray.colors(10, start = 0.3, end = 0.9, gamma = 2.2, alpha = NULL), useRaster = TRUE, bty='n')
#plot shape
plot(RISE, xlim=c(minxlim, maxxlim), ylim=c(minylim, maxylim), col=alpha(c("green", "yellow", "orange", "red")[RISE$z_Werte_21], 0.3), border="white", lwd=0.001, add=T)
I am pretty new to R and I don't know how to solve the problem, when plotting the raster the layers do not overlap properly, when imaging the raster the map gets distorted. Is there anybody who had this problem before or who has a solution for the problem?