rworldmap package - Keeping the lower xlim in a sequence of maps, but the left regions are changing

StackOverflow https://stackoverflow.com/questions/20483931

  •  30-08-2022
  •  | 
  •  

Question

I'm trying to create an animation composed of a sequence of maps. At one stage of this animation I needed to keep the left extremity of the map and only extend it far right. To do so I kept the xlim lower limit fixed and changed only the xlim upper limit.

library(rworldmap)

worldMap <- getMap(resolution = "high")

fixedLatitude <- c(36.76, 38.76)

# Fixed lower limit of the X axis
fixedMinLongitude <- 20.64
# Varying the upper limit of the X axis 
maxLongitudes <- seq(22.64, 22.78498, length.out = 4)
longitudes <- lapply(maxLongitudes, function(x) c(fixedMinLongitude, x))

countMaps <- 1
for (ln in longitudes){
  png(paste("test", countMaps, ".png", sep = ""))
  mapCountryData(worldMap,
                 xlim = ln,
                 ylim = fixedLatitude,
                 addLegend = F, mapTitle = "")

  dev.off()
  countMaps <- countMaps + 1
}

I expected that the regions to the left of the map would not change in the four figures. That is, the contours of these would not be cut off from the map. But the result I got was this.

In these sequences it can be seen that the edge of the island on the left is being changed because the map is "walking" to the right and not only expading to the right as I expected to.

Where am I going wrong?

A more extreme example (with vertical lines)

library(rworldmap)

worldMap <- getMap(resolution = "high")

fixedLatitude <- c(36.76, 38.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))

proportionLeftSpace <- seq(0,0, length.out = 4)
countMaps <- 1
for (ln in longitudes){
  png(paste("test", countMaps, ".png", sep = ""))
  mapCountryData(worldMap,
                  xlim = ln,
                  ylim = fixedLatitude,
                  addLegend = F, mapTitle = "")
 abline(v = ln)

 plotRegionMinX <- par("usr")[1]
 spaceBeforeXlim <-  ln[1] -  plotRegionMinX
 onePercentXAxis <- diff(ln)/100
 proportionLeftSpace[countMaps] <- spaceBeforeXlim/onePercentXAxis 

 dev.off()
 countMaps <- countMaps + 1
}

proportionLeftSpace

The result.

In this example the transition between the first and the second map have the problem I had mentioned, however, the transition between the other maps is what I desire.

Following the suggestion of Andy, I added vertical lines. This showed me that there is a greater distance between the lower threshold of the xlim and the "plot region" in the first map. To corroborate this I added the proportionLeftSpace variable that stores the percentage of space left before bottom xlim. The result of it is:

39.28339  4.00000  4.00000  4.00000

So, there is a 10 times greater space before the xlim in the first map than in the others.

Was it helpful?

Solution

@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.

larger x border because ylim determines scale in upper two 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 : x border constant because xlim determines scale in all plots

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top