0

I am trying to plot interactive maps at the NUTS3 level. Initially I was using shapefiles that I constructed myself for the data in QGIS, however, when I tried to use them in R they rendered poorly with lines or even entire polygons being warped out of place/shape. I then tried using the shapefiles provided by Eurostat directly (which my shapefiles were large based off of) using the Eurostat package, however, when I plot the map there are still lines/polygons that are being warped out of position (different ones from those of my original shapefiles, however).

I have what I believe should be a reproducible example showcasing the issue below:

# Get relevant NUTS3 shapefile from Eurostat    
NUTS3EU <- get_eurostat_geospatial(output_class = "df", resolution = "60")

# Generate random number vector
x <- rnorm(n = nrow(NUTS3EU), mean = 1)

# Merge random number vector into data as new variable 'RandVar'
NUTS3EU$RandVar <- x

# Subset data down to just Germany NUTS3 level
deuNUTS <- NUTS3EU[(NUTS3EU$CNTR_CODE == "DE"),]
deuNUTS <- deuNUTS[(deuNUTS$LEVL_CODE == 3),]

# Generate ggplot object
userMap2 <- ggplot(deuNUTS, aes(long, lat, group = NUTS_ID, fill = RandVar, data_id = NUTS_ID, tooltip = id)) + geom_polygon_interactive(colour = "white", size = 0.3)

# Generate Girafe interactive plot object
ggiraph(ggobj = userMap2, tooltip_offy = -20, zoom_max = 3)

# Output should be map of Germany with small number of lines/polygons being warped, may need to zoom in to see the issue

I understand that the 'order' variable is important and have made sure it is correctly sorted, but the issue persists.

How can I make the plot geographically correct?

If I need to use a different source for the shapefile that is fine as long as it is accurate.

Image of plot output showing issues with lines and polygons

2
  • Why get the data as "df" and then try and make spatial data out of it when get_eurostat_geospatial can get them in sf format which is a spatial format designed for mapping?
    – Spacedman
    Feb 22, 2021 at 13:09
  • The function ggplot() wants a data.frame and will not work with other objects. I found out when I initially tried to use a shape file, which does work with the basic plot() command. But to have an interactive map I seem to need to use ggplot(). Feb 22, 2021 at 13:51

1 Answer 1

0

I managed to figure out the simple mistake I was making. I was grouping the polygons by the same unique ID that my data refers to. However, many of the polygons are disconnected or even just constructed in multiple pieces, these disconnects are identified by a different sub-group in the data frame and when I group by that variable it fixes the aesthetic issue.

Edited code with desired output below:

NUTS3EU_df <- get_eurostat_geospatial(output_class = "df", resolution = "60", nuts_level = 3, year = 2016)

x <- rnorm(n = nrow(NUTS3EU_df), mean = 1)

NUTS3EU_df$RandVar <- x

deuNUTS <- NUTS3EU_df[(NUTS3EU_df$CNTR_CODE == "DE"),]
deuNUTS <- deuNUTS[(deuNUTS$LEVL_CODE == 3),]

userMap1 <- ggplot(deuNUTS, aes(long, lat, group = group, fill = RandVar, data_id = NUTS_ID, tooltip = id)) + geom_polygon_interactive(colour = "white", size = 0.3)

ggiraph(ggobj = userMap1, tooltip_offy = -20, zoom_max = 3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.