3

我正在尝试创建由一系列地图组成的动画。在这个动画的一个阶段,我需要保持地图的左端,并且只将它延伸到最右边。为此,我保持xlim下限固定,只更改xlim上限。

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
}

我预计地图左侧的区域在四个数字中不会改变。也就是说,这些轮廓的轮廓不会从地图上截断。但我得到的结果是这样的。

在这些序列中,可以看到左侧岛屿的边缘正在发生变化,因为地图正在向右“行走”,而不仅仅是像我预期的那样向右扩展。

我哪里错了?

一个更极端的例子(带有垂直线)

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

结果。

在这个例子中,第一张和第二张地图之间的过渡有我提到的问题,但是,其他地图之间的过渡是我想要的。

按照安迪的建议,我添加了垂直线。xlim这向我表明,第一张地图中的下限阈值与“绘图区域”之间的距离更大。为了证实这一点,我添加了proportionLeftSpace 存储底部之前剩余空间百分比的变量xlim。它的结果是:

39.28339  4.00000  4.00000  4.00000

因此,在第一张地图中,在 之前的空间是其他地图的 10 倍xlim

4

1 回答 1

0

@celacanto 太棒了!感谢您对问题的澄清,并帮助我准确了解 R & 因此如何rworldmap处理情节范围。

对您的问题的简短回答:xlim在您的第一个绘图中,左侧和左侧绘图边界之间的间隙较大,因为ylim相对于 较大xlim,因此决定了绘图的规模。这在下图中进行了演示,其中ylim确定了上 2 个图中xlim的比例并确定了下 2 个图中的比例。

更大的 x 边界,因为 ylim 确定了上两个图中的比例

解决方案是ylim相对于下面的代码中显示的更小xlim,这应该意味着左侧绘图边框不会像此绘图中那样移动: x 边界常数,因为 xlim 确定所有图中的比例

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

在这个例子proportionLeftSpace中,作为常数 4,4,4,4 出现,这是 R 设置的默认值。

希望有帮助,祝你好运!

于 2013-12-10T20:46:32.707 回答