How to create a map of Denmark with ggmap












12















I have installed the following two packages:



library(ggmap)
library(maps)


I have found code online to create a world map (see below). How do I zoom the plot on a country level? For example, Denmark.



ggplot()+
borders("world", colour="gray50", fill="gray50")


Enter image description here










share|improve this question













migrated from stackoverflow.com 14 hours ago


This question came from our site for professional and enthusiast programmers.























    12















    I have installed the following two packages:



    library(ggmap)
    library(maps)


    I have found code online to create a world map (see below). How do I zoom the plot on a country level? For example, Denmark.



    ggplot()+
    borders("world", colour="gray50", fill="gray50")


    Enter image description here










    share|improve this question













    migrated from stackoverflow.com 14 hours ago


    This question came from our site for professional and enthusiast programmers.





















      12












      12








      12








      I have installed the following two packages:



      library(ggmap)
      library(maps)


      I have found code online to create a world map (see below). How do I zoom the plot on a country level? For example, Denmark.



      ggplot()+
      borders("world", colour="gray50", fill="gray50")


      Enter image description here










      share|improve this question














      I have installed the following two packages:



      library(ggmap)
      library(maps)


      I have found code online to create a world map (see below). How do I zoom the plot on a country level? For example, Denmark.



      ggplot()+
      borders("world", colour="gray50", fill="gray50")


      Enter image description here







      r ggplot2 ggmap






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      DavidDavid

      614




      614




      migrated from stackoverflow.com 14 hours ago


      This question came from our site for professional and enthusiast programmers.









      migrated from stackoverflow.com 14 hours ago


      This question came from our site for professional and enthusiast programmers.
























          2 Answers
          2






          active

          oldest

          votes


















          15














          If we look at ?borders and, for more into, ?map, we see that we may use the regions argument:



          ggplot() + borders(regions = "Denmark", colour = "gray50", fill = "gray50") 


          enter image description here






          share|improve this answer



















          • 1





            Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

            – JonnyCrunch
            yesterday











          • @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

            – David
            yesterday






          • 2





            @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

            – Julius Vainora
            yesterday






          • 1





            When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

            – JAD
            yesterday



















          8














          Get the bounds of Denmark in lat-long and use coord_fixed:



          ggplot() + borders("world", colour="gray50", fill="gray50") + coord_fixed(xlim=c(7, 12), ylim=c(52, 58))


          Enter image description here



          You can get the bounds from the map package:



          > map("world", "Denmark", plot=FALSE)$range
          [1] 8.121484 15.137110 54.628857 57.736916


          And you might want to expand these a bit for nicer spacing and more context.






          share|improve this answer
























          • @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

            – David
            yesterday











          • Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

            – Spacedman
            yesterday











          • @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

            – David
            yesterday






          • 1





            Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

            – Spacedman
            yesterday






          • 4





            @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

            – David
            yesterday











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fgis.stackexchange.com%2fquestions%2f310474%2fhow-to-create-a-map-of-denmark-with-ggmap%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









          15














          If we look at ?borders and, for more into, ?map, we see that we may use the regions argument:



          ggplot() + borders(regions = "Denmark", colour = "gray50", fill = "gray50") 


          enter image description here






          share|improve this answer



















          • 1





            Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

            – JonnyCrunch
            yesterday











          • @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

            – David
            yesterday






          • 2





            @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

            – Julius Vainora
            yesterday






          • 1





            When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

            – JAD
            yesterday
















          15














          If we look at ?borders and, for more into, ?map, we see that we may use the regions argument:



          ggplot() + borders(regions = "Denmark", colour = "gray50", fill = "gray50") 


          enter image description here






          share|improve this answer



















          • 1





            Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

            – JonnyCrunch
            yesterday











          • @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

            – David
            yesterday






          • 2





            @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

            – Julius Vainora
            yesterday






          • 1





            When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

            – JAD
            yesterday














          15












          15








          15







          If we look at ?borders and, for more into, ?map, we see that we may use the regions argument:



          ggplot() + borders(regions = "Denmark", colour = "gray50", fill = "gray50") 


          enter image description here






          share|improve this answer













          If we look at ?borders and, for more into, ?map, we see that we may use the regions argument:



          ggplot() + borders(regions = "Denmark", colour = "gray50", fill = "gray50") 


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday







          Julius Vainora















          • 1





            Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

            – JonnyCrunch
            yesterday











          • @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

            – David
            yesterday






          • 2





            @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

            – Julius Vainora
            yesterday






          • 1





            When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

            – JAD
            yesterday














          • 1





            Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

            – JonnyCrunch
            yesterday











          • @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

            – David
            yesterday






          • 2





            @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

            – Julius Vainora
            yesterday






          • 1





            When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

            – JAD
            yesterday








          1




          1





          Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

          – JonnyCrunch
          yesterday





          Also, a very beautiful package for maps is leaflet: i.e l <- leaflet() %>% setView(lat = 50.85045, lng = 4.34878, zoom=5) %>% addTiles(group="OSM")

          – JonnyCrunch
          yesterday













          @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

          – David
          yesterday





          @Julius Vainora: Thanks. Can I add layers to this plot? For example if I find a data set with "state boundries"?

          – David
          yesterday




          2




          2





          @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

          – Julius Vainora
          yesterday





          @David, yes, layers can be added, for instance + geom_point(data = data.frame(x = 12, y = 56), aes(x = x, y = y)) adds a point.

          – Julius Vainora
          yesterday




          1




          1





          When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

          – JAD
          yesterday





          When using maps, you might want to add coord_cartesian(), so that the scale of the x and the y axes match.

          – JAD
          yesterday













          8














          Get the bounds of Denmark in lat-long and use coord_fixed:



          ggplot() + borders("world", colour="gray50", fill="gray50") + coord_fixed(xlim=c(7, 12), ylim=c(52, 58))


          Enter image description here



          You can get the bounds from the map package:



          > map("world", "Denmark", plot=FALSE)$range
          [1] 8.121484 15.137110 54.628857 57.736916


          And you might want to expand these a bit for nicer spacing and more context.






          share|improve this answer
























          • @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

            – David
            yesterday











          • Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

            – Spacedman
            yesterday











          • @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

            – David
            yesterday






          • 1





            Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

            – Spacedman
            yesterday






          • 4





            @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

            – David
            yesterday
















          8














          Get the bounds of Denmark in lat-long and use coord_fixed:



          ggplot() + borders("world", colour="gray50", fill="gray50") + coord_fixed(xlim=c(7, 12), ylim=c(52, 58))


          Enter image description here



          You can get the bounds from the map package:



          > map("world", "Denmark", plot=FALSE)$range
          [1] 8.121484 15.137110 54.628857 57.736916


          And you might want to expand these a bit for nicer spacing and more context.






          share|improve this answer
























          • @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

            – David
            yesterday











          • Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

            – Spacedman
            yesterday











          • @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

            – David
            yesterday






          • 1





            Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

            – Spacedman
            yesterday






          • 4





            @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

            – David
            yesterday














          8












          8








          8







          Get the bounds of Denmark in lat-long and use coord_fixed:



          ggplot() + borders("world", colour="gray50", fill="gray50") + coord_fixed(xlim=c(7, 12), ylim=c(52, 58))


          Enter image description here



          You can get the bounds from the map package:



          > map("world", "Denmark", plot=FALSE)$range
          [1] 8.121484 15.137110 54.628857 57.736916


          And you might want to expand these a bit for nicer spacing and more context.






          share|improve this answer













          Get the bounds of Denmark in lat-long and use coord_fixed:



          ggplot() + borders("world", colour="gray50", fill="gray50") + coord_fixed(xlim=c(7, 12), ylim=c(52, 58))


          Enter image description here



          You can get the bounds from the map package:



          > map("world", "Denmark", plot=FALSE)$range
          [1] 8.121484 15.137110 54.628857 57.736916


          And you might want to expand these a bit for nicer spacing and more context.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          SpacedmanSpacedman

          23.1k23549




          23.1k23549













          • @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

            – David
            yesterday











          • Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

            – Spacedman
            yesterday











          • @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

            – David
            yesterday






          • 1





            Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

            – Spacedman
            yesterday






          • 4





            @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

            – David
            yesterday



















          • @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

            – David
            yesterday











          • Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

            – Spacedman
            yesterday











          • @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

            – David
            yesterday






          • 1





            Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

            – Spacedman
            yesterday






          • 4





            @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

            – David
            yesterday

















          @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

          – David
          yesterday





          @ when I run map("world","Denmark",plot=FALSE)$range I get NULL?

          – David
          yesterday













          Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

          – Spacedman
          yesterday





          Odd. Does map("world","Denmark") produce a map of Denmark? What about "France" or "Belgium"? Is it using local country names ("Danmark")? Does map("world") draw a map? Does map("world",plot=FALSE)$names return a vector of country/region names? If those don't work, then that's very odd and you should ask a new Q or report a bug...

          – Spacedman
          yesterday













          @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

          – David
          yesterday





          @ Spacedan: None of them work either NULL or error. But I can create the plots in the answers...

          – David
          yesterday




          1




          1





          Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

          – Spacedman
          yesterday





          Start a new clean R session, do library(maps), then try? Maybe you've got something masking the maps::map function. Probably purrr::map? Don't use library(tidyverse).

          – Spacedman
          yesterday




          4




          4





          @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

          – David
          yesterday





          @ Spacedman: I think it is okay if I use maps::map. Maybe it thought the map function from the purrr package...

          – David
          yesterday


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • 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%2fgis.stackexchange.com%2fquestions%2f310474%2fhow-to-create-a-map-of-denmark-with-ggmap%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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]