I am trying to convert an Eurostat's geojson file here to a dataframe using packages geojsonio
and broom
, but when the file is converted into the dataframe using the broom::tidy()
function many of the columns in the geojson file are not converted and when I create a map with ggplot
the map is not correct. I need the geojson to be in a dataframe.
There's a way of getting geojson data converted into a dataframe
from Eurostat with eurostat
package, but the problem is that the eurostat function get_eurostat_geospatial()
only gets map data for Europe and I need the world map - Eurostat has the world map, but is not retreivable using get_eurostat_geospatial()
.
My question is: how can I efficiently convert a geojson file into a dataframe and keep all features in the geojson?
This the format I need, based on get_eurostat_geospatial()
function:
library(eurostat)
map = get_eurostat_geospatial(output_class = "df", resolution = 20, nuts_level = 0, year = 2016, crs = "4326")
# A tibble: 6,588 × 17
long lat order hole piece group id NUTS_ID LEVL_CODE CNTR_CODE NAME_LATN NUTS_NAME MOUNT_TYPE URBN_TYPE COAST_TYPE FID
<dbl> <dbl> <int> <lgl> <fct> <fct> <chr> <chr> <int> <chr> <chr> <chr> <int> <int> <int> <chr>
1 -7.03 43.5 1 FALSE 1 1.1 1 ES 0 ES ESPAÑA ESPAÑA 0 0 0 ES
2 -6.29 43.6 2 FALSE 1 1.1 1 ES 0 ES ESPAÑA ESPAÑA 0 0 0 ES
And his is the code I am using to convert the geojson to dataframe and produce the map without success:
library(tidyverse)
library(broom)
library(geojsonio)
file = "LOCATION OF THE GEOJSON FILE/CNTR_BN_20M_2020_4326.shp"
df = geojson_read(file, what = "sp")
df_tidy = tidy(df)
ggplot() +
geom_polygon(data = df_tidy, aes(x=long, y=lat, group=group))