R ggmap center around 180 degrees longitude
I am mapping points falling around 180 degrees longitude using R ang ggmap (see example below).
The map is mostly empty. Is there a way to center the map around longitude = 180 and limit the extent to where the points are?
In other words, I'm interested in limiting the range on the x-axis to 180±14 degrees.
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)
r ggmap
add a comment |
I am mapping points falling around 180 degrees longitude using R ang ggmap (see example below).
The map is mostly empty. Is there a way to center the map around longitude = 180 and limit the extent to where the points are?
In other words, I'm interested in limiting the range on the x-axis to 180±14 degrees.
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)
r ggmap
There's a typo here: longitude = 180. Isn't it latitude?
– Rui Barradas
Nov 22 at 18:33
Longitude is correct, I'm interested in limiting the range on the x-axis to 180±14
– Claudia
Nov 22 at 18:43
In principal, you can control the x-axis using+ coord_fixed(xlim=c(value1,value2))
. However, glueing together the points to the left and the right is a bit harder to accomplish. Maybe this thread is of help: stackoverflow.com/questions/11201997/world-map-with-ggmap
– Mr. Zen
Nov 22 at 20:09
When you say 180±14, do you want to see 166, ..., 180, -1, ..., -14 in the x-axis? Or is 166, ..., 194 also acceptable?
– Julius Vainora
Nov 22 at 20:17
Ideally, I'd like to see 166, ..., 180, -180...-170 on the x-axis. This is for consistency with other maps. I suppose I could also plot the range 166-194 and then change the labels on the x-axis, any suggestions on how to achieve this with get_stamenmap?
– Claudia
Nov 29 at 10:47
add a comment |
I am mapping points falling around 180 degrees longitude using R ang ggmap (see example below).
The map is mostly empty. Is there a way to center the map around longitude = 180 and limit the extent to where the points are?
In other words, I'm interested in limiting the range on the x-axis to 180±14 degrees.
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)
r ggmap
I am mapping points falling around 180 degrees longitude using R ang ggmap (see example below).
The map is mostly empty. Is there a way to center the map around longitude = 180 and limit the extent to where the points are?
In other words, I'm interested in limiting the range on the x-axis to 180±14 degrees.
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)
r ggmap
r ggmap
edited Nov 22 at 18:49
asked Nov 22 at 17:58
Claudia
4591519
4591519
There's a typo here: longitude = 180. Isn't it latitude?
– Rui Barradas
Nov 22 at 18:33
Longitude is correct, I'm interested in limiting the range on the x-axis to 180±14
– Claudia
Nov 22 at 18:43
In principal, you can control the x-axis using+ coord_fixed(xlim=c(value1,value2))
. However, glueing together the points to the left and the right is a bit harder to accomplish. Maybe this thread is of help: stackoverflow.com/questions/11201997/world-map-with-ggmap
– Mr. Zen
Nov 22 at 20:09
When you say 180±14, do you want to see 166, ..., 180, -1, ..., -14 in the x-axis? Or is 166, ..., 194 also acceptable?
– Julius Vainora
Nov 22 at 20:17
Ideally, I'd like to see 166, ..., 180, -180...-170 on the x-axis. This is for consistency with other maps. I suppose I could also plot the range 166-194 and then change the labels on the x-axis, any suggestions on how to achieve this with get_stamenmap?
– Claudia
Nov 29 at 10:47
add a comment |
There's a typo here: longitude = 180. Isn't it latitude?
– Rui Barradas
Nov 22 at 18:33
Longitude is correct, I'm interested in limiting the range on the x-axis to 180±14
– Claudia
Nov 22 at 18:43
In principal, you can control the x-axis using+ coord_fixed(xlim=c(value1,value2))
. However, glueing together the points to the left and the right is a bit harder to accomplish. Maybe this thread is of help: stackoverflow.com/questions/11201997/world-map-with-ggmap
– Mr. Zen
Nov 22 at 20:09
When you say 180±14, do you want to see 166, ..., 180, -1, ..., -14 in the x-axis? Or is 166, ..., 194 also acceptable?
– Julius Vainora
Nov 22 at 20:17
Ideally, I'd like to see 166, ..., 180, -180...-170 on the x-axis. This is for consistency with other maps. I suppose I could also plot the range 166-194 and then change the labels on the x-axis, any suggestions on how to achieve this with get_stamenmap?
– Claudia
Nov 29 at 10:47
There's a typo here: longitude = 180. Isn't it latitude?
– Rui Barradas
Nov 22 at 18:33
There's a typo here: longitude = 180. Isn't it latitude?
– Rui Barradas
Nov 22 at 18:33
Longitude is correct, I'm interested in limiting the range on the x-axis to 180±14
– Claudia
Nov 22 at 18:43
Longitude is correct, I'm interested in limiting the range on the x-axis to 180±14
– Claudia
Nov 22 at 18:43
In principal, you can control the x-axis using
+ coord_fixed(xlim=c(value1,value2))
. However, glueing together the points to the left and the right is a bit harder to accomplish. Maybe this thread is of help: stackoverflow.com/questions/11201997/world-map-with-ggmap– Mr. Zen
Nov 22 at 20:09
In principal, you can control the x-axis using
+ coord_fixed(xlim=c(value1,value2))
. However, glueing together the points to the left and the right is a bit harder to accomplish. Maybe this thread is of help: stackoverflow.com/questions/11201997/world-map-with-ggmap– Mr. Zen
Nov 22 at 20:09
When you say 180±14, do you want to see 166, ..., 180, -1, ..., -14 in the x-axis? Or is 166, ..., 194 also acceptable?
– Julius Vainora
Nov 22 at 20:17
When you say 180±14, do you want to see 166, ..., 180, -1, ..., -14 in the x-axis? Or is 166, ..., 194 also acceptable?
– Julius Vainora
Nov 22 at 20:17
Ideally, I'd like to see 166, ..., 180, -180...-170 on the x-axis. This is for consistency with other maps. I suppose I could also plot the range 166-194 and then change the labels on the x-axis, any suggestions on how to achieve this with get_stamenmap?
– Claudia
Nov 29 at 10:47
Ideally, I'd like to see 166, ..., 180, -180...-170 on the x-axis. This is for consistency with other maps. I suppose I could also plot the range 166-194 and then change the labels on the x-axis, any suggestions on how to achieve this with get_stamenmap?
– Claudia
Nov 29 at 10:47
add a comment |
2 Answers
2
active
oldest
votes
Could not figure out how to do this using ggmap.
However, here is the solution using the ggplot and maps packages (modified version of the code here: 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")
add a comment |
It's more an issue with stamen
and data structures than with anything else. Not a problem with Google Maps and a bit of data wrangling.
Code:
# 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)
Explanation:
If you use zoom = 1
with the current Google Maps API it gives you a bit of longitude overlap left and right which you can cut to your liking.
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
If you check the boundaries of the raw map above you see that we have total 450 degrees of longitude (44.64844 + 405.35156) that we can use. Therefore, we can cut down our map to lon = c(0, 360)
and lat = c(-70, 70)
(using scale_x_continuous
and scale_y_continuous
, respectively) which will leave the center point at exactly lon = 180
and lat = 0
.
Your data, however, uses the right longitude values of 0:-180 (West)
and 0:180 (East)
. Therefore, we also need to update the longitude values that are below zero to the corresponding value in a 0:360 system. This is achieved by 180 + 180 + lon
(so longitude = -172
will turn into longitude = 188
for the purpose of plotting).
Visualization:
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436165%2fr-ggmap-center-around-180-degrees-longitude%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Could not figure out how to do this using ggmap.
However, here is the solution using the ggplot and maps packages (modified version of the code here: 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")
add a comment |
Could not figure out how to do this using ggmap.
However, here is the solution using the ggplot and maps packages (modified version of the code here: 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")
add a comment |
Could not figure out how to do this using ggmap.
However, here is the solution using the ggplot and maps packages (modified version of the code here: 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")
Could not figure out how to do this using ggmap.
However, here is the solution using the ggplot and maps packages (modified version of the code here: 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")
edited Nov 23 at 10:50
answered Nov 23 at 9:29
Claudia
4591519
4591519
add a comment |
add a comment |
It's more an issue with stamen
and data structures than with anything else. Not a problem with Google Maps and a bit of data wrangling.
Code:
# 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)
Explanation:
If you use zoom = 1
with the current Google Maps API it gives you a bit of longitude overlap left and right which you can cut to your liking.
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
If you check the boundaries of the raw map above you see that we have total 450 degrees of longitude (44.64844 + 405.35156) that we can use. Therefore, we can cut down our map to lon = c(0, 360)
and lat = c(-70, 70)
(using scale_x_continuous
and scale_y_continuous
, respectively) which will leave the center point at exactly lon = 180
and lat = 0
.
Your data, however, uses the right longitude values of 0:-180 (West)
and 0:180 (East)
. Therefore, we also need to update the longitude values that are below zero to the corresponding value in a 0:360 system. This is achieved by 180 + 180 + lon
(so longitude = -172
will turn into longitude = 188
for the purpose of plotting).
Visualization:
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
add a comment |
It's more an issue with stamen
and data structures than with anything else. Not a problem with Google Maps and a bit of data wrangling.
Code:
# 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)
Explanation:
If you use zoom = 1
with the current Google Maps API it gives you a bit of longitude overlap left and right which you can cut to your liking.
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
If you check the boundaries of the raw map above you see that we have total 450 degrees of longitude (44.64844 + 405.35156) that we can use. Therefore, we can cut down our map to lon = c(0, 360)
and lat = c(-70, 70)
(using scale_x_continuous
and scale_y_continuous
, respectively) which will leave the center point at exactly lon = 180
and lat = 0
.
Your data, however, uses the right longitude values of 0:-180 (West)
and 0:180 (East)
. Therefore, we also need to update the longitude values that are below zero to the corresponding value in a 0:360 system. This is achieved by 180 + 180 + lon
(so longitude = -172
will turn into longitude = 188
for the purpose of plotting).
Visualization:
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
add a comment |
It's more an issue with stamen
and data structures than with anything else. Not a problem with Google Maps and a bit of data wrangling.
Code:
# 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)
Explanation:
If you use zoom = 1
with the current Google Maps API it gives you a bit of longitude overlap left and right which you can cut to your liking.
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
If you check the boundaries of the raw map above you see that we have total 450 degrees of longitude (44.64844 + 405.35156) that we can use. Therefore, we can cut down our map to lon = c(0, 360)
and lat = c(-70, 70)
(using scale_x_continuous
and scale_y_continuous
, respectively) which will leave the center point at exactly lon = 180
and lat = 0
.
Your data, however, uses the right longitude values of 0:-180 (West)
and 0:180 (East)
. Therefore, we also need to update the longitude values that are below zero to the corresponding value in a 0:360 system. This is achieved by 180 + 180 + lon
(so longitude = -172
will turn into longitude = 188
for the purpose of plotting).
Visualization:
It's more an issue with stamen
and data structures than with anything else. Not a problem with Google Maps and a bit of data wrangling.
Code:
# 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)
Explanation:
If you use zoom = 1
with the current Google Maps API it gives you a bit of longitude overlap left and right which you can cut to your liking.
> g$data
lon lat
1 -44.64844 -87.75631
2 405.35156 -87.75631
3 -44.64844 87.72862
4 405.35156 87.72862
If you check the boundaries of the raw map above you see that we have total 450 degrees of longitude (44.64844 + 405.35156) that we can use. Therefore, we can cut down our map to lon = c(0, 360)
and lat = c(-70, 70)
(using scale_x_continuous
and scale_y_continuous
, respectively) which will leave the center point at exactly lon = 180
and lat = 0
.
Your data, however, uses the right longitude values of 0:-180 (West)
and 0:180 (East)
. Therefore, we also need to update the longitude values that are below zero to the corresponding value in a 0:360 system. This is achieved by 180 + 180 + lon
(so longitude = -172
will turn into longitude = 188
for the purpose of plotting).
Visualization:
edited Nov 24 at 19:52
answered Nov 24 at 19:19
Roman
1,556221
1,556221
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
add a comment |
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Thanks for the detailed explanation. Unfortunately I am not allowed to use Google Maps. What is the problem with stamen?
– Claudia
Nov 29 at 10:45
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
Stamen wants a boundary box with min_longitude < max_longitude (and a value range of -180 to +180). There is no way I know you can achieve what you want with the current API / call method without having to stitch stuff together.
– Roman
yesterday
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436165%2fr-ggmap-center-around-180-degrees-longitude%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There's a typo here: longitude = 180. Isn't it latitude?
– Rui Barradas
Nov 22 at 18:33
Longitude is correct, I'm interested in limiting the range on the x-axis to 180±14
– Claudia
Nov 22 at 18:43
In principal, you can control the x-axis using
+ coord_fixed(xlim=c(value1,value2))
. However, glueing together the points to the left and the right is a bit harder to accomplish. Maybe this thread is of help: stackoverflow.com/questions/11201997/world-map-with-ggmap– Mr. Zen
Nov 22 at 20:09
When you say 180±14, do you want to see 166, ..., 180, -1, ..., -14 in the x-axis? Or is 166, ..., 194 also acceptable?
– Julius Vainora
Nov 22 at 20:17
Ideally, I'd like to see 166, ..., 180, -180...-170 on the x-axis. This is for consistency with other maps. I suppose I could also plot the range 166-194 and then change the labels on the x-axis, any suggestions on how to achieve this with get_stamenmap?
– Claudia
Nov 29 at 10:47