How to create a map of Denmark with ggmap












13















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 Jan 30 at 15:07


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























    13















    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 Jan 30 at 15:07


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





















      13












      13








      13








      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 Jan 29 at 12:27









      DavidDavid

      737




      737




      migrated from stackoverflow.com Jan 30 at 15:07


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









      migrated from stackoverflow.com Jan 30 at 15:07


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
























          2 Answers
          2






          active

          oldest

          votes


















          16














          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
            Jan 29 at 12:35











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

            – David
            Jan 29 at 12:40






          • 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
            Jan 29 at 12:42






          • 1





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

            – JAD
            Jan 29 at 21:44



















          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
            Jan 29 at 12:42











          • 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
            Jan 29 at 12:59











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

            – David
            Jan 29 at 13:04






          • 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
            Jan 29 at 13:05






          • 4





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

            – David
            Jan 29 at 13:05











          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









          16














          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
            Jan 29 at 12:35











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

            – David
            Jan 29 at 12:40






          • 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
            Jan 29 at 12:42






          • 1





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

            – JAD
            Jan 29 at 21:44
















          16














          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
            Jan 29 at 12:35











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

            – David
            Jan 29 at 12:40






          • 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
            Jan 29 at 12:42






          • 1





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

            – JAD
            Jan 29 at 21:44














          16












          16








          16







          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 Jan 29 at 12:34







          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
            Jan 29 at 12:35











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

            – David
            Jan 29 at 12:40






          • 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
            Jan 29 at 12:42






          • 1





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

            – JAD
            Jan 29 at 21:44














          • 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
            Jan 29 at 12:35











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

            – David
            Jan 29 at 12:40






          • 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
            Jan 29 at 12:42






          • 1





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

            – JAD
            Jan 29 at 21:44








          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
          Jan 29 at 12:35





          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
          Jan 29 at 12:35













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

          – David
          Jan 29 at 12:40





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

          – David
          Jan 29 at 12:40




          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
          Jan 29 at 12:42





          @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
          Jan 29 at 12:42




          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
          Jan 29 at 21:44





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

          – JAD
          Jan 29 at 21:44













          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
            Jan 29 at 12:42











          • 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
            Jan 29 at 12:59











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

            – David
            Jan 29 at 13:04






          • 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
            Jan 29 at 13:05






          • 4





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

            – David
            Jan 29 at 13:05
















          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
            Jan 29 at 12:42











          • 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
            Jan 29 at 12:59











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

            – David
            Jan 29 at 13:04






          • 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
            Jan 29 at 13:05






          • 4





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

            – David
            Jan 29 at 13:05














          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 Jan 29 at 12:35









          SpacedmanSpacedman

          23.7k23549




          23.7k23549













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

            – David
            Jan 29 at 12:42











          • 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
            Jan 29 at 12:59











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

            – David
            Jan 29 at 13:04






          • 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
            Jan 29 at 13:05






          • 4





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

            – David
            Jan 29 at 13:05



















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

            – David
            Jan 29 at 12:42











          • 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
            Jan 29 at 12:59











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

            – David
            Jan 29 at 13:04






          • 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
            Jan 29 at 13:05






          • 4





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

            – David
            Jan 29 at 13:05

















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

          – David
          Jan 29 at 12:42





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

          – David
          Jan 29 at 12:42













          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
          Jan 29 at 12:59





          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
          Jan 29 at 12:59













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

          – David
          Jan 29 at 13:04





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

          – David
          Jan 29 at 13:04




          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
          Jan 29 at 13:05





          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
          Jan 29 at 13:05




          4




          4





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

          – David
          Jan 29 at 13:05





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

          – David
          Jan 29 at 13:05


















          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

          Probability when a professor distributes a quiz and homework assignment to a class of n students.

          Aardman Animations

          Are they similar matrix