gpt4 book ai didi

rworldmap 包 - 将较低的 xlim 保留在一系列 map 中,但左侧区域正在变化

转载 作者:行者123 更新时间:2023-12-02 04:57:16 27 4
gpt4 key购买 nike

我正在尝试创建一个由一系列 map 组成的动画。在这个动画的一个阶段,我需要保留 map 的左端,只将它延伸到最右边。为此,我保持 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
}

我预计 map 左侧的区域在四个数字中不会发生变化。也就是说,这些轮廓不会从 map 上切掉。但是 result I got was this .

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

我哪里错了?

更极端的例子(有竖线)

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.

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

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

39.28339  4.00000  4.00000  4.00000

因此,第一张 map 中 xlim 之前的空间比其他 map 大 10 倍。

最佳答案

@celacanto 太棒了!感谢您对问题的澄清,并帮助我准确理解 R 以及 rworldmap 如何处理绘图范围。

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

larger x border because ylim determines scale in upper two plots

解决方案是使 ylim 相对于 xlim 更小,如下面的代码所示,这应该意味着左图边框不会像该图中那样移动: 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

在此示例中,proportionLeftSpace 显示为常量 4,4,4,4,这是 R 的默认设置。

希望对您有所帮助,祝您好运!

关于rworldmap 包 - 将较低的 xlim 保留在一系列 map 中,但左侧区域正在变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com