R ggmap中心在180度经度附近
R ggmap center around 180 degrees longitude
我正在使用 R ang ggmap 映射落在经度 180 度左右的点(参见下面的示例)。
地图大部分是空的。有没有办法将地图以经度 = 180 为中心并将范围限制在点所在的位置?
换句话说,我有兴趣将 x 轴上的范围限制在 180±14 度。
library("ggmap")
x <- structure(list(Bias = structure(c(5L, 4L, 3L, 3L, 4L, 6L, 6L,
3L, 3L, 4L, 3L, 5L, 2L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L,
6L, 5L, 4L, 3L, 6L, 5L, 4L,
5L, 4L, 4L, 3L, 6L, 4L, 6L,
4L, 5L, 4L, 4L, 4L, 3L, 3L,
4L, 2L, 4L, 3L, 3L, 6L, 4L,
5L),
.Label = c("(-Inf,-5]",
"(-5,-2]",
"(-2,0]",
"(0,2]",
"(2,5]",
"(5, Inf]"),
class = "factor"),
lat = c(-1.35, -13.24, -14.31, -16.13, -17.15,
-17.35, -17.75, -18.05, -18.23, -20.67,
-29.24, -34.43, -35.13, -35.9, -37.01,
-37.56, -37.67, -38.66, -38.74, -39.01,
-39.45, -39.47, -40.32, -40.54, -40.9,
-40.9, -41.3, -41.33, -41.73, -41.74,
-42.42, -42.71, -43.49, -44.3, -45.02,
-45.22, -45.93, -46.16, -46.41, -47.28,
-50.49, -52.55, -43.86, -18.15, -12.5,
-19.05, -52.55, -44.53, -38.17, -17.23),
lon = c(176, -176.19, -178.12, -179.98, 176.9,
178.22, 177.45, 178.57, -178.8, -178.72,
-177.93, 172.68, 174.02, 175.12, 174.81,
178.31, 176.2, 177.99, 176.08, 174.18,
175.66, 176.86, 175.61, 173, 174.99,
176.21, 173.22, 174.81, 174.28, 171.58,
173.7, 170.98, 172.53, 171.22, 168.74,
166.88, 170.2, 166.61, 168.32, 167.46,
166.3, 169.15, 169.01, 177.42, 177.05,
178.17, 169.13, 169.89, 174.7, -178.95)),
.Names = c("Bias", "lat", "lon"),
class = "data.frame",
row.names = c(NA, -50L))
# Map extent
xy <- c(left = min(x$lon), bottom = min(x$lat),
right = max(x$lon), top = max(x$lat))
# Download the base map
gg <- get_stamenmap(xy, zoom = 5, maptype = "toner-lite")
ggmap(gg) +
geom_point(data = x, aes(x = lon, y = lat, col = Bias),
size = 1, alpha = 0.9) +
scale_color_viridis(discrete = T)
无法弄清楚如何使用 ggmap 执行此操作。
但是,这里是使用 ggplot 和 maps 包的解决方案(此处代码的修改版本:World map with ggmap)。
library(maps)
library(ggplot2)
# x is the data.frame defined in the question
# Recenter points
center <- 180
# shift coordinates to recenter x
x$long.recenter <- ifelse(x$lon < center - 180 , x$lon + 360, x$lon)
# shift coordinates to recenter worldmap
worldmap <- map_data ("world", wrap = c(0, 360))
# Plot worldmap using data from worldmap.cp
ggplot(aes(x = long, y = lat), data = worldmap) +
geom_polygon(aes(group = group), fill="#f9f9f9", colour = "grey65") +
scale_y_continuous(limits = c(-60, 0)) +
scale_x_continuous(limits = c(180 - 20, 180 + 15),
breaks = seq(160, 190, 10),
labels = c(160, 170, "180/-180", -170)) +
coord_equal() + theme_bw() +
geom_point(data = x,
aes(x = long.recenter, y = lat, col = Bias),
pch = 19, size = 3, alpha = .4) +
xlab("Longitude") + ylab("Latitude")
stamen
和数据结构的问题比其他任何问题都大。 Google 地图和一些数据争论不是问题。
代码:
# Using your data as basis
x[x$lon < 0, ]$lon <- 2*180 + x[x$lon < 0, ]$lon
# Get map with lon center opposite the Meridian
g <- ggmap(get_googlemap(c(180, 0), zoom = 1), extent = "panel")
# Plot map with boundaries
g + scale_x_continuous(limits = c(0, 360), expand = c(0,0)) +
scale_y_continuous(limits = c(-70, 70), expand = c(0,0)) +
# Plot data points
geom_point(data = x, aes(x = lon, y = lat, color = Bias),
size = 1, alpha = 0.9) +
# Add custom color scheme
scale_color_viridis(discrete = T)
解释:
如果您将 zoom = 1
与当前的 Google 地图 API 一起使用,它会给您带来一些经度左右重叠,您可以根据自己的喜好进行切割。
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
如果您查看上面原始地图的边界,您会发现我们可以使用总共 450 度的经度 (44.64844 + 405.35156)。因此,我们可以将地图缩小到 lon = c(0, 360)
和 lat = c(-70, 70)
(分别使用 scale_x_continuous
和 scale_y_continuous
),这将使中心点恰好位于 lon = 180
和lat = 0
.
但是,您的数据使用正确的经度值 0:-180 (West)
和 0:180 (East)
。因此,我们还需要将零以下的经度值更新为0:360系统中的相应值。这是通过 180 + 180 + lon
来实现的(所以 longitude = -172
会变成 longitude = 188
为了绘图的目的)。
可视化:
我正在使用 R ang ggmap 映射落在经度 180 度左右的点(参见下面的示例)。 地图大部分是空的。有没有办法将地图以经度 = 180 为中心并将范围限制在点所在的位置? 换句话说,我有兴趣将 x 轴上的范围限制在 180±14 度。
library("ggmap")
x <- structure(list(Bias = structure(c(5L, 4L, 3L, 3L, 4L, 6L, 6L,
3L, 3L, 4L, 3L, 5L, 2L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L,
6L, 5L, 4L, 3L, 6L, 5L, 4L,
5L, 4L, 4L, 3L, 6L, 4L, 6L,
4L, 5L, 4L, 4L, 4L, 3L, 3L,
4L, 2L, 4L, 3L, 3L, 6L, 4L,
5L),
.Label = c("(-Inf,-5]",
"(-5,-2]",
"(-2,0]",
"(0,2]",
"(2,5]",
"(5, Inf]"),
class = "factor"),
lat = c(-1.35, -13.24, -14.31, -16.13, -17.15,
-17.35, -17.75, -18.05, -18.23, -20.67,
-29.24, -34.43, -35.13, -35.9, -37.01,
-37.56, -37.67, -38.66, -38.74, -39.01,
-39.45, -39.47, -40.32, -40.54, -40.9,
-40.9, -41.3, -41.33, -41.73, -41.74,
-42.42, -42.71, -43.49, -44.3, -45.02,
-45.22, -45.93, -46.16, -46.41, -47.28,
-50.49, -52.55, -43.86, -18.15, -12.5,
-19.05, -52.55, -44.53, -38.17, -17.23),
lon = c(176, -176.19, -178.12, -179.98, 176.9,
178.22, 177.45, 178.57, -178.8, -178.72,
-177.93, 172.68, 174.02, 175.12, 174.81,
178.31, 176.2, 177.99, 176.08, 174.18,
175.66, 176.86, 175.61, 173, 174.99,
176.21, 173.22, 174.81, 174.28, 171.58,
173.7, 170.98, 172.53, 171.22, 168.74,
166.88, 170.2, 166.61, 168.32, 167.46,
166.3, 169.15, 169.01, 177.42, 177.05,
178.17, 169.13, 169.89, 174.7, -178.95)),
.Names = c("Bias", "lat", "lon"),
class = "data.frame",
row.names = c(NA, -50L))
# Map extent
xy <- c(left = min(x$lon), bottom = min(x$lat),
right = max(x$lon), top = max(x$lat))
# Download the base map
gg <- get_stamenmap(xy, zoom = 5, maptype = "toner-lite")
ggmap(gg) +
geom_point(data = x, aes(x = lon, y = lat, col = Bias),
size = 1, alpha = 0.9) +
scale_color_viridis(discrete = T)
无法弄清楚如何使用 ggmap 执行此操作。 但是,这里是使用 ggplot 和 maps 包的解决方案(此处代码的修改版本:World map with ggmap)。
library(maps)
library(ggplot2)
# x is the data.frame defined in the question
# Recenter points
center <- 180
# shift coordinates to recenter x
x$long.recenter <- ifelse(x$lon < center - 180 , x$lon + 360, x$lon)
# shift coordinates to recenter worldmap
worldmap <- map_data ("world", wrap = c(0, 360))
# Plot worldmap using data from worldmap.cp
ggplot(aes(x = long, y = lat), data = worldmap) +
geom_polygon(aes(group = group), fill="#f9f9f9", colour = "grey65") +
scale_y_continuous(limits = c(-60, 0)) +
scale_x_continuous(limits = c(180 - 20, 180 + 15),
breaks = seq(160, 190, 10),
labels = c(160, 170, "180/-180", -170)) +
coord_equal() + theme_bw() +
geom_point(data = x,
aes(x = long.recenter, y = lat, col = Bias),
pch = 19, size = 3, alpha = .4) +
xlab("Longitude") + ylab("Latitude")
stamen
和数据结构的问题比其他任何问题都大。 Google 地图和一些数据争论不是问题。
代码:
# Using your data as basis
x[x$lon < 0, ]$lon <- 2*180 + x[x$lon < 0, ]$lon
# Get map with lon center opposite the Meridian
g <- ggmap(get_googlemap(c(180, 0), zoom = 1), extent = "panel")
# Plot map with boundaries
g + scale_x_continuous(limits = c(0, 360), expand = c(0,0)) +
scale_y_continuous(limits = c(-70, 70), expand = c(0,0)) +
# Plot data points
geom_point(data = x, aes(x = lon, y = lat, color = Bias),
size = 1, alpha = 0.9) +
# Add custom color scheme
scale_color_viridis(discrete = T)
解释:
如果您将 zoom = 1
与当前的 Google 地图 API 一起使用,它会给您带来一些经度左右重叠,您可以根据自己的喜好进行切割。
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
如果您查看上面原始地图的边界,您会发现我们可以使用总共 450 度的经度 (44.64844 + 405.35156)。因此,我们可以将地图缩小到 lon = c(0, 360)
和 lat = c(-70, 70)
(分别使用 scale_x_continuous
和 scale_y_continuous
),这将使中心点恰好位于 lon = 180
和lat = 0
.
但是,您的数据使用正确的经度值 0:-180 (West)
和 0:180 (East)
。因此,我们还需要将零以下的经度值更新为0:360系统中的相应值。这是通过 180 + 180 + lon
来实现的(所以 longitude = -172
会变成 longitude = 188
为了绘图的目的)。