0

I am trying to create an interactive map of Germany with plotly and ggplot. At last, I'd like to overlay geom_points with tooltips, but so far I am struggeling with the aspect ratio.

I create the plot like this:

library(tidyverse)
library(ggplot2)
library(plotly)

p <- map_data("world") %>%
  filter(region=="Germany") %>%
  ggplot(aes(x=long, y = lat, group = group)) +
  geom_polygon(fill="grey") +
  coord_map()

When simply plotting it with ggplot, the aspect ratio of Germany is how I know it from school.

p

germany_with_correct_aspect_ration

When converting it to a plotly plot with ggplotly the plot is distorted.

ggplotly(p)

enter image description here

Any ideas how to fix it? I don't know whether this is relevant, but I used to run this code in an R Markdown document.

Thanks a lot.

kato-m
  • 113
  • 1
  • 1
  • 5

1 Answers1

0

I'll guess you can't get a decent level of detail from the world map you used. With maps there are basically two types: generic, and, GIS (real mapping data).

Below is a non-distorted map of Germany. Here are the steps and data sources.

Download the GIS data - map of Germany. Source, https://gadm.org/download_country_v3.html Choose the level of detail. 0 - the outline of the country, 1 - the country w/states. These files will have, sp.rds , extensions.

Cut and Paste the downloaded geographical reference data file into your working directory - this is easier than creating a path from the working directory to some other folder location. Since this data is actual geospatial data used in creating this plot of Germany, this data/plot can be used as a base map or overlay map with google maps, Overstreet maps, etc..

Use the following libraries and two lines of code to create the plot of Germany.

library(maptools)
library(raster)
library(rgdal)

gadm <- readRDS("gadm36_DEU_0_sp.rds")   # get the gis file from w/d.
plot(gadm)

enter image description here

Gray
  • 1,164
  • 1
  • 9
  • 23
  • 1
    Thanks for you answer. However, it didn't solve my problem. I followed your proposal and used GIS data. I converted it to a data.frame to plot it with `ggplot` and the result was as your image not distorted, but as soon as I converted the `ggplot`-plot to a `plotly`-plot with `ggplotly`, the map was distorted. In general, the generic map would be fine for me, I just have the impression that `ggplotly` messes up the aspect ration as described in my question. – kato-m Jun 20 '20 at 16:15