How to shift coordinates in ggplot2 map for any arbitrary longitude range












2















I'm making a map using ggplot and I want to be able to center the map around the Pacific Ocean while plotting points on the map.



enter image description here



It turns out I can do the map shifting by using the wrap option of maps::map. However, I'm not sure exactly how to shift the points to get them to match to my new shifted map. I found how to do that when I recreate a Pacific centered map with wrap(0, 360) but I'm not sure how to accomplish this for arbitrary shift units. I'm sure this is pretty straightforward but I can't seem to figure it out. Any ideas?



library(maps)
library(tidyverse)

# Pacific centered map
shift_value_1 <- 0
shift_value_2 <- 360

# Regular map, how about new values of shift_value_1 and shift_value_2? (e.g. -20, 325)
shift_value_1 <- -180
shift_value_2 <- 180


map_world_df <- map_data('world', wrap=c(shift_value_1, shift_value_2)) %>%
dplyr::filter(region != "Antarctica")


country_shapes <- geom_polygon(data = map_world_df,
aes(x=long, y = lat, group = group),
fill = "gainsboro",
color = "gainsboro",
size = 0.15)

nodes <- data.frame(names = c("A", "B", "C", "D"),
lat = c(64.220241, 10.278386, 64.710869, 19.432564),
lon = c(135.75572, 34.33927, -151.20003, -99.13323))


nodes$lon[nodes$lon <0] <- nodes$lon[nodes$lon <0] + (shift_value_1 + shift_value_2)

ggplot() +
country_shapes +
geom_point(data = nodes, aes(x=lon, y = lat))


enter image description here



I would love to be able to have a way of selecting arbitrary longitude ranges and shift the points accordingly, instead I get incorrectly placed points.










share|improve this question





























    2















    I'm making a map using ggplot and I want to be able to center the map around the Pacific Ocean while plotting points on the map.



    enter image description here



    It turns out I can do the map shifting by using the wrap option of maps::map. However, I'm not sure exactly how to shift the points to get them to match to my new shifted map. I found how to do that when I recreate a Pacific centered map with wrap(0, 360) but I'm not sure how to accomplish this for arbitrary shift units. I'm sure this is pretty straightforward but I can't seem to figure it out. Any ideas?



    library(maps)
    library(tidyverse)

    # Pacific centered map
    shift_value_1 <- 0
    shift_value_2 <- 360

    # Regular map, how about new values of shift_value_1 and shift_value_2? (e.g. -20, 325)
    shift_value_1 <- -180
    shift_value_2 <- 180


    map_world_df <- map_data('world', wrap=c(shift_value_1, shift_value_2)) %>%
    dplyr::filter(region != "Antarctica")


    country_shapes <- geom_polygon(data = map_world_df,
    aes(x=long, y = lat, group = group),
    fill = "gainsboro",
    color = "gainsboro",
    size = 0.15)

    nodes <- data.frame(names = c("A", "B", "C", "D"),
    lat = c(64.220241, 10.278386, 64.710869, 19.432564),
    lon = c(135.75572, 34.33927, -151.20003, -99.13323))


    nodes$lon[nodes$lon <0] <- nodes$lon[nodes$lon <0] + (shift_value_1 + shift_value_2)

    ggplot() +
    country_shapes +
    geom_point(data = nodes, aes(x=lon, y = lat))


    enter image description here



    I would love to be able to have a way of selecting arbitrary longitude ranges and shift the points accordingly, instead I get incorrectly placed points.










    share|improve this question



























      2












      2








      2








      I'm making a map using ggplot and I want to be able to center the map around the Pacific Ocean while plotting points on the map.



      enter image description here



      It turns out I can do the map shifting by using the wrap option of maps::map. However, I'm not sure exactly how to shift the points to get them to match to my new shifted map. I found how to do that when I recreate a Pacific centered map with wrap(0, 360) but I'm not sure how to accomplish this for arbitrary shift units. I'm sure this is pretty straightforward but I can't seem to figure it out. Any ideas?



      library(maps)
      library(tidyverse)

      # Pacific centered map
      shift_value_1 <- 0
      shift_value_2 <- 360

      # Regular map, how about new values of shift_value_1 and shift_value_2? (e.g. -20, 325)
      shift_value_1 <- -180
      shift_value_2 <- 180


      map_world_df <- map_data('world', wrap=c(shift_value_1, shift_value_2)) %>%
      dplyr::filter(region != "Antarctica")


      country_shapes <- geom_polygon(data = map_world_df,
      aes(x=long, y = lat, group = group),
      fill = "gainsboro",
      color = "gainsboro",
      size = 0.15)

      nodes <- data.frame(names = c("A", "B", "C", "D"),
      lat = c(64.220241, 10.278386, 64.710869, 19.432564),
      lon = c(135.75572, 34.33927, -151.20003, -99.13323))


      nodes$lon[nodes$lon <0] <- nodes$lon[nodes$lon <0] + (shift_value_1 + shift_value_2)

      ggplot() +
      country_shapes +
      geom_point(data = nodes, aes(x=lon, y = lat))


      enter image description here



      I would love to be able to have a way of selecting arbitrary longitude ranges and shift the points accordingly, instead I get incorrectly placed points.










      share|improve this question
















      I'm making a map using ggplot and I want to be able to center the map around the Pacific Ocean while plotting points on the map.



      enter image description here



      It turns out I can do the map shifting by using the wrap option of maps::map. However, I'm not sure exactly how to shift the points to get them to match to my new shifted map. I found how to do that when I recreate a Pacific centered map with wrap(0, 360) but I'm not sure how to accomplish this for arbitrary shift units. I'm sure this is pretty straightforward but I can't seem to figure it out. Any ideas?



      library(maps)
      library(tidyverse)

      # Pacific centered map
      shift_value_1 <- 0
      shift_value_2 <- 360

      # Regular map, how about new values of shift_value_1 and shift_value_2? (e.g. -20, 325)
      shift_value_1 <- -180
      shift_value_2 <- 180


      map_world_df <- map_data('world', wrap=c(shift_value_1, shift_value_2)) %>%
      dplyr::filter(region != "Antarctica")


      country_shapes <- geom_polygon(data = map_world_df,
      aes(x=long, y = lat, group = group),
      fill = "gainsboro",
      color = "gainsboro",
      size = 0.15)

      nodes <- data.frame(names = c("A", "B", "C", "D"),
      lat = c(64.220241, 10.278386, 64.710869, 19.432564),
      lon = c(135.75572, 34.33927, -151.20003, -99.13323))


      nodes$lon[nodes$lon <0] <- nodes$lon[nodes$lon <0] + (shift_value_1 + shift_value_2)

      ggplot() +
      country_shapes +
      geom_point(data = nodes, aes(x=lon, y = lat))


      enter image description here



      I would love to be able to have a way of selecting arbitrary longitude ranges and shift the points accordingly, instead I get incorrectly placed points.







      r ggplot2 maps






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 4:54









      Z.Lin

      11.6k21835




      11.6k21835










      asked Jan 1 at 6:46









      RecRec

      132




      132
























          1 Answer
          1






          active

          oldest

          votes


















          1














          First of all, when using wrapping with two limits, you should make sure they add up correctly. Wrapping is not the same as just setting boundaries. For instance, in you're comment to the code you are asking the map to continue from -25 to 320, which is inconsistent. The resulting map will have some strange artefacts (more visible if you try larger errors, e.g. (-25, 150) ). You should always have shift_value2 - shift_value1 == 360, so for instance (-25, 335). Shifting longitudes by any other value will always give errors. You can add xlim=c(-25, 320) to the map() call if you wish, of course. But this should be done in a separate map() call (as explained in the documentation: xlim is applied before the wrapping, so combining them results in part of the map being dropped). So for a limited map, you should probably do



          mymap <- map(wrap=c(-25, 335), fill=TRUE, plot=FALSE)
          map(mymap, xlim=c(-25, 250),...)


          But that is not necessary when using ggplot2 for plotting the map, because those map limits are applied after the call to map() and the wrapping.



          Shifting an arbitrary point just means adding (or subtracting) 360 until it falls between the two values. In most cases, the following should work:



          lon[lon < shift_value1] <- lon[lon < shift_value1] + 360
          lon[lon > shift_value2] <- lon[lon > shift_value2] - 360





          share|improve this answer


























          • Thank you Alex! This is exactly what I needed.

            – Rec
            Jan 2 at 14:25











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993548%2fhow-to-shift-coordinates-in-ggplot2-map-for-any-arbitrary-longitude-range%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          First of all, when using wrapping with two limits, you should make sure they add up correctly. Wrapping is not the same as just setting boundaries. For instance, in you're comment to the code you are asking the map to continue from -25 to 320, which is inconsistent. The resulting map will have some strange artefacts (more visible if you try larger errors, e.g. (-25, 150) ). You should always have shift_value2 - shift_value1 == 360, so for instance (-25, 335). Shifting longitudes by any other value will always give errors. You can add xlim=c(-25, 320) to the map() call if you wish, of course. But this should be done in a separate map() call (as explained in the documentation: xlim is applied before the wrapping, so combining them results in part of the map being dropped). So for a limited map, you should probably do



          mymap <- map(wrap=c(-25, 335), fill=TRUE, plot=FALSE)
          map(mymap, xlim=c(-25, 250),...)


          But that is not necessary when using ggplot2 for plotting the map, because those map limits are applied after the call to map() and the wrapping.



          Shifting an arbitrary point just means adding (or subtracting) 360 until it falls between the two values. In most cases, the following should work:



          lon[lon < shift_value1] <- lon[lon < shift_value1] + 360
          lon[lon > shift_value2] <- lon[lon > shift_value2] - 360





          share|improve this answer


























          • Thank you Alex! This is exactly what I needed.

            – Rec
            Jan 2 at 14:25
















          1














          First of all, when using wrapping with two limits, you should make sure they add up correctly. Wrapping is not the same as just setting boundaries. For instance, in you're comment to the code you are asking the map to continue from -25 to 320, which is inconsistent. The resulting map will have some strange artefacts (more visible if you try larger errors, e.g. (-25, 150) ). You should always have shift_value2 - shift_value1 == 360, so for instance (-25, 335). Shifting longitudes by any other value will always give errors. You can add xlim=c(-25, 320) to the map() call if you wish, of course. But this should be done in a separate map() call (as explained in the documentation: xlim is applied before the wrapping, so combining them results in part of the map being dropped). So for a limited map, you should probably do



          mymap <- map(wrap=c(-25, 335), fill=TRUE, plot=FALSE)
          map(mymap, xlim=c(-25, 250),...)


          But that is not necessary when using ggplot2 for plotting the map, because those map limits are applied after the call to map() and the wrapping.



          Shifting an arbitrary point just means adding (or subtracting) 360 until it falls between the two values. In most cases, the following should work:



          lon[lon < shift_value1] <- lon[lon < shift_value1] + 360
          lon[lon > shift_value2] <- lon[lon > shift_value2] - 360





          share|improve this answer


























          • Thank you Alex! This is exactly what I needed.

            – Rec
            Jan 2 at 14:25














          1












          1








          1







          First of all, when using wrapping with two limits, you should make sure they add up correctly. Wrapping is not the same as just setting boundaries. For instance, in you're comment to the code you are asking the map to continue from -25 to 320, which is inconsistent. The resulting map will have some strange artefacts (more visible if you try larger errors, e.g. (-25, 150) ). You should always have shift_value2 - shift_value1 == 360, so for instance (-25, 335). Shifting longitudes by any other value will always give errors. You can add xlim=c(-25, 320) to the map() call if you wish, of course. But this should be done in a separate map() call (as explained in the documentation: xlim is applied before the wrapping, so combining them results in part of the map being dropped). So for a limited map, you should probably do



          mymap <- map(wrap=c(-25, 335), fill=TRUE, plot=FALSE)
          map(mymap, xlim=c(-25, 250),...)


          But that is not necessary when using ggplot2 for plotting the map, because those map limits are applied after the call to map() and the wrapping.



          Shifting an arbitrary point just means adding (or subtracting) 360 until it falls between the two values. In most cases, the following should work:



          lon[lon < shift_value1] <- lon[lon < shift_value1] + 360
          lon[lon > shift_value2] <- lon[lon > shift_value2] - 360





          share|improve this answer















          First of all, when using wrapping with two limits, you should make sure they add up correctly. Wrapping is not the same as just setting boundaries. For instance, in you're comment to the code you are asking the map to continue from -25 to 320, which is inconsistent. The resulting map will have some strange artefacts (more visible if you try larger errors, e.g. (-25, 150) ). You should always have shift_value2 - shift_value1 == 360, so for instance (-25, 335). Shifting longitudes by any other value will always give errors. You can add xlim=c(-25, 320) to the map() call if you wish, of course. But this should be done in a separate map() call (as explained in the documentation: xlim is applied before the wrapping, so combining them results in part of the map being dropped). So for a limited map, you should probably do



          mymap <- map(wrap=c(-25, 335), fill=TRUE, plot=FALSE)
          map(mymap, xlim=c(-25, 250),...)


          But that is not necessary when using ggplot2 for plotting the map, because those map limits are applied after the call to map() and the wrapping.



          Shifting an arbitrary point just means adding (or subtracting) 360 until it falls between the two values. In most cases, the following should work:



          lon[lon < shift_value1] <- lon[lon < shift_value1] + 360
          lon[lon > shift_value2] <- lon[lon > shift_value2] - 360






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 1:09

























          answered Jan 1 at 23:53









          Alex DeckmynAlex Deckmyn

          804410




          804410













          • Thank you Alex! This is exactly what I needed.

            – Rec
            Jan 2 at 14:25



















          • Thank you Alex! This is exactly what I needed.

            – Rec
            Jan 2 at 14:25

















          Thank you Alex! This is exactly what I needed.

          – Rec
          Jan 2 at 14:25





          Thank you Alex! This is exactly what I needed.

          – Rec
          Jan 2 at 14:25




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993548%2fhow-to-shift-coordinates-in-ggplot2-map-for-any-arbitrary-longitude-range%23new-answer', 'question_page');
          }
          );

          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







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas