@celacanto Excellent! Thanks for the clarification in the question and helping me to understand exactly how R & therefore rworldmap
deals with plot extents.
The short answer to your question :
There is a bigger gap between the left xlim
and the left plot boundary in your first plot because the ylim
is larger relative to the xlim
and therefore determining the scale of the plot. This is demonstrated in the fig below where ylim
determines the scale in the upper 2 plots and xlim
determines the scale in the lower 2 plots.
The solution is to make ylim
smaller relative to xlim
as shown in the code below and that should mean that the left plot border will not move as in this plot :
library(rworldmap)
#worldMap <- getMap(resolution = "high")
#this makes it quicker & illustrates the same thing
worldMap <- getMap(resolution="low")
#fixedLatitude <- c(36.76, 38.76)
#making the latitudinal difference smaller
#so that the longitudinal difference always determines map scale
fixedLatitude <- c(36.76, 37.76)
# Fixed lower limit of the X axis
fixedMinLongitude <- 20.64
# Varying the upper limit of the X axis
maxLongitudes <- seq(22, 25, length.out = 4)
longitudes <- lapply(maxLongitudes, function(x) c(fixedMinLongitude, x))
#create blank object to store left space
proportionLeftSpace <- seq(0,0, length.out = 4)
#alternative to put all 4 plots together on windows, comment out png & dev.off
#windows()
#op <- par(mfrow = c(2, 2))
countMaps <- 1
for (ln in longitudes){
png(paste("xlimtest", countMaps, ".png", sep = ""))
mapCountryData(worldMap,
xlim = ln,
ylim = fixedLatitude,
addLegend = F, mapTitle = "")
abline(v=ln)
abline(h=fixedLatitude)
#from celacanto
plotRegionMinX <- par("usr")[1]
spaceBeforeXlim <- ln[1] - plotRegionMinX
onePercentXAxis <- diff(ln)/100
proportionLeftSpace[countMaps] <- spaceBeforeXlim/onePercentXAxis
dev.off()
countMaps <- countMaps + 1
}
proportionLeftSpace
In this example proportionLeftSpace
comes out as a constant 4,4,4,4 which is the default set by R.
Hope that helps and good luck!